繁体   English   中英

LocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)不正确的位置

[英]LocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER) incorrect Location

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公里

因为getLastKnownLocation不返回当前位置。 它返回通过该提供者找出的最后一个位置。 该职位可能已有一个小时的历史-如果有职位,它将仍然返回。 如果要获取准确的位置,则需要通过调用requestUpdates或requestSingleUpdate来打开位置提供程序,然后等待onLocationChanged函数至少调用一次。 其他可能是过时的位置。

这是因为您使用的是getLastKnownLocation(): location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

getLastKnownLocation(String Provider):返回一个位置,该位置指示来自从给定提供者获得的最新已知位置修复的数据。

这可以在不启动提供程序的情况下完成。 请注意,该位置可能已过时,例如,如果设备已关闭并移动到另一个位置。

来自: http : //developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation%28java.lang.String%29

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM