但实际上我有点困惑,我必须在我的代码中放置getLastKnownLocation(String)方法! 在requestLocationUpdates()之前或之后?在我的函数或onResume()??
请任何人帮助我,提前谢谢
这是我的功能: -
private final LocationListener gpsLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
locationManager.removeUpdates(networkLocationListener);
Toast.makeText(activity,"New GPS location: "
+ String.format("%9.6f", location.getLatitude()) + ", "
+ String.format("%9.6f", location.getLongitude()) + "\n",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
Toast.makeText(activity,"GPS available again\n",Toast.LENGTH_SHORT).show();
break;
case LocationProvider.OUT_OF_SERVICE:
Toast.makeText(activity,"GPS out of service\n",Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Toast.makeText(activity,"GPS temporarily unavailable\n", Toast.LENGTH_SHORT).show();
break;
}}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(activity,"GPS Provider Enabled\n",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(activity,"GPS Provider Disabled\n",Toast.LENGTH_SHORT).show();
}
};
private final LocationListener networkLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Toast.makeText(activity,"New network location: "
+ String.format("%9.6f", location.getLatitude()) + ", "
+ String.format("%9.6f", location.getLongitude()) + "\n",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
Toast.makeText(activity,"Network location available again\n",Toast.LENGTH_SHORT).show();
break;
case LocationProvider.OUT_OF_SERVICE:
Toast.makeText(activity,"Network location out of service\n",Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Toast.makeText(activity,"Network location temporarily unavailable\n",Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(activity,"Network Provider Enabled\n",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(activity,"Network Provider Disabled\n",Toast.LENGTH_SHORT).show();
}
};
这是onResume()函数中的代码: -
locationManager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000, 0, gpsLocationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 5000, 0,
networkLocationListener);
答案 0 :(得分:0)
lastKnownLocation()
之前调用{p> requestLocationUpdates()
,但我注意到,一旦获取了某个位置,就会调用removeUpdates()
。也许您想要使用requestSingleUpdate
代替?
https://developer.android.com/reference/android/location/LocationManager.html#requestSingleUpdate(java.lang.String,android.app.PendingIntent)
应该注意的是,在this answer他们说你应该等待GPS稳定并避免使用requestSingleUpdate()
,但如果你没有达到很高的准确度,那就更好了。