【问题标题】:Location not being updated automatically位置未自动更新
【发布时间】:2013-01-26 00:00:29
【问题描述】:

我在我的 Fragment 类中实现 LocationListener 并使用以下代码来跟踪位置更新,尽管它没有得到应有的频繁更新。我需要像 Google Navigator 这样在地图上显示用户位置。

寻找最好的供应商

mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

// if I uncomment the next line, it takes too long to get the location
// criteria.setAccuracy(Criteria.ACCURACY_FINE);
// if I leave the line above commented, the app seems to receiving location from triangulation

mProvider = mLocationManager.getBestProvider(criteria, false);
Location location = mLocationManager.getLastKnowLocation(mProvider);

LocationChanged 事件

@Override
public void onLocationChanged(Location location) {
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

    setMapCameraPosition(latLng);
}

【问题讨论】:

  • 您是否使用 MapView 来显示用户位置?
  • 我正在使用 Google Maps Android API v2。

标签: android android-location


【解决方案1】:

您只要求最后一个已知位置,它可能存在也可能不存在。要使用 LocationListener,您必须调用 requestLocationUpdates() 方法之一。

mLocationManager.requestLocationUpdates(mProvider, 0, 0, mLocationListener);

【讨论】:

  • 我想补充一点,使用一个小过滤器,只有在新位置更准确时才更新用户位置(newLocation.getAccuracy()
  • @Machinarius 当我没有设置精度时,应用程序只使用三角测量而不是 GPS。
  • @Sam 实际上我正在使用 onResume 方法中的 mLLocationManager.requestLocationUpdates(mProvider, 1000, 1, this)。
  • 好的,它多久更新一次,你要走多远?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 1970-01-01
  • 1970-01-01
  • 2022-07-23
相关资源
最近更新 更多