public Location getLastKnownLocation() {
LocationManager mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null
|| l.getAccuracy() < bestLocation.getAccuracy()) {
bestLocation = l;
}
}
if (bestLocation == null) {
return null;
}
return bestLocation;
}为什么这个函数会返回错误的位置??误差约为8-9公里
发布于 2014-07-08 17:11:58
因为getLastKnownLocation不返回当前位置。它返回通过该提供程序计算出的最后一个位置。这个位置可能已经有一个小时了--如果有的话,它仍然会返回它。如果您希望获得准确的位置,则需要通过调用requestUpdates或requestSingleUpdate打开位置提供程序,然后等待onLocationChanged函数至少被调用一次。其他任何内容都可能是过时的位置。
发布于 2014-07-08 17:12:09
这是因为您正在使用getLastKnownLocation():location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
getLastKnownLocation(String Provider):返回一个位置,该位置指示从给定提供程序获得的最后一个已知位置修复中的数据。
这可以在不启动提供程序的情况下完成。请注意,此位置可能已过期,例如,如果设备已关闭并移动到另一个位置。
来自:http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29
https://stackoverflow.com/questions/24627863
复制相似问题