我想在Android应用程序中找到我当前位置的纬度和经度。当我用手机稳定时,我只需要它。
我的代码是:
LocationManager locationManager;
locationManager = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation
(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,0,0,(LocationListener) this);
updateWithNewLocation(location);
}
private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
String latLongString;
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString);
}
在模拟器上运行后,我总是找到“找不到位置”。为什么会这样?
答案 0 :(得分:14)
public class GPSmap extends Activity {
GeoPoint geoPoint;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocationManager locManager;
setContentView(R.layout.main);
locManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
500.0f, locationListener);
Location location = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
}
}
private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView) findViewById(R.id.text);
String latLongString = "";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" + latLongString);
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider,int status,Bundle extras){}
};
}
如果您将使用此代码,您将得到答案。
答案 1 :(得分:4)
GPS收音机并非一直处于打开状态,因为它会严重耗尽电池电量。在您拨打getLastKnownLocation()
时,收音机可能已关闭。
您需要先执行requestLocationUpdates()
,然后等待修复程序提供给LocationListener
。在此之后的任何时间(removeUpdates()
),getLastKnownLocation()
都应返回非null
值。
答案 2 :(得分:2)
我有很多教程,但发现了最好的教程:
使用Criteria
和BestProvider
/** PROCESS for Get Longitude and Latitude **/
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Log.d("msg", "GPS:"+isGPSEnabled);
// check if GPS enabled
if(isGPSEnabled){
/*
longitude = 70.80079728674089;
latitude = 22.29090332494221;
*/
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
//new LoadPlaces().execute();
//Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null)
{
longitude = location.getLongitude();
latitude = location.getLatitude();
Log.d("msg", "first lat long : "+latitude +" "+ longitude);
//new LoadPlaces().execute();
}else
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
longitude = location.getLongitude();
latitude = location.getLatitude();
Log.d("msg", "changed lat long : "+latitude +" "+ longitude);
}
});
}
}
else
{
showAlertforGPS();
}
答案 3 :(得分:1)
默认情况下,模拟器可能没有任何关联的位置。所以它没有给予任何。
要发送位置,请转到Eclipse中的DDMS透视图,然后输入纬度和经度并单击“发送”。然后,您将能够在您的应用程序中看到它。
如果您还有任何问题,请与我们联系。