首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)位置不正确

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

Stack Overflow用户
提问于 2014-07-08 17:08:05
回答 2查看 430关注 0票数 0
代码语言:javascript
复制
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公里

EN

回答 2

Stack Overflow用户

发布于 2014-07-08 17:11:58

因为getLastKnownLocation不返回当前位置。它返回通过该提供程序计算出的最后一个位置。这个位置可能已经有一个小时了--如果有的话,它仍然会返回它。如果您希望获得准确的位置,则需要通过调用requestUpdates或requestSingleUpdate打开位置提供程序,然后等待onLocationChanged函数至少被调用一次。其他任何内容都可能是过时的位置。

票数 0
EN

Stack Overflow用户

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24627863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档