【发布时间】:2012-01-03 10:47:32
【问题描述】:
我已经四处寻找,但找不到任何关于我正在寻找的直接线索。我正在尝试创建一个 Android 应用程序,该应用程序在按下按钮时拨出紧急号码(我已经开始工作)但无法显示位置(以经度和纬度显示),我尝试使用 Toast 和编辑文本框。我是 Android 开发的新手,所以想从简单的东西开始,但是 LongLat 部分很麻烦。任何帮助将不胜感激。
下面是我一直在篡改的代码,以便尝试获取 Long 和 Lat,然后在另一个文件中,我一直在尝试使用点击侦听器将其分配给按钮,以便当按钮为按下(在 main.xml 中),它将在文本字段或 toast 中显示 Long 和 Lat。
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.TextView;
import android.content.Context;
import android.location.LocationManager;
import android.location.Criteria;
public class EmergencyLocation extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
latituteField = (TextView) findViewById(R.id.TextView);
longitudeField = (TextView) findViewById(R.id.long_lat);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialise the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
} else {
latituteField.setText("Provider not available");
longitudeField.setText("Provider not available");
}
}
private void TextView() {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}}
【问题讨论】:
-
贴出你的代码...这将有助于理解...
-
那是我得到的代码并试图适应。
标签: android location latitude-longitude