【问题标题】:onLocationChanged() is called too many times, even when the device hasn't moveonLocationChanged() 被调用太多次,即使设备没有移动
【发布时间】:2015-10-28 12:43:42
【问题描述】:

我已将 OnLocationChanged(Location location) 实现为我的 Activity 中的覆盖方法,如图所示:

@Override
public void onLocationChanged(Location location) {

    mCurrentLocation = location;
    if (wifiSpotList != null) {
        presenter.onLocationUpdated(wifiSpotList, mCurrentLocation);
        centerInLoc(location);
    }
}

问题是,即使设备没有移动,这个方法也会被触发。它总是以毫秒为间隔发生。

通过位置参数的调试器让我得到这个日志:

mTime = 1446035851646
mLongitude = -3.6923793
mLatitude = 40.5008861

mTime = 1446035856976
mLongitude = -3.6923596
mLatitude = 40.5008932

mTime = 1446035867327
mLongitude = -3.6923681
mLatitude = 40.5008987

mTime = 1446035877344
mLongitude = -3.6923778
mLatitude = 40.5008949

mTime = 1446035882442
mLongitude = -3.6923734
mLatitude = 40.5008926

...

编辑:

这是初始化 requestLocationUpdates 的代码:

protected void startLocationUpdates() {
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    this.locationUpdatesStarted = true;
}

编辑 mLLocationRequest:

protected void createLocationRequest() {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(LOCATION_REQUEST_INTERVAL);
    mLocationRequest.setFastestInterval(LOCATION_REQUEST_FASTEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    this.mLocationRequest = mLocationRequest;
}

该方法是因为经纬度变化而触发的吗? 为什么即使设备没有移动它也会触发? 我可以限制准确性吗? 有什么干净的方法来实现 onLocationChanged() 吗?

【问题讨论】:

  • 告诉我你调用的代码requestLocationUpdates()我想你已经设置了时间间隔。 requestLocationUpdates() 方法中的第二个参数。
  • 我已将其添加到问题中。
  • 您是否已将setInterval() 添加到您的mLocationRequest 变量中?
  • 我也加了。
  • 检查 LOCATION_REQUEST_INTERVAL 将被分配给一个整数值,它是毫秒。对于LOCATION_REQUEST_INTERVAL 的每个间隔时间段,它将更新位置,请参阅此link

标签: android coding-style google-maps-android-api-2


【解决方案1】:
setSmallestDisplacement(float smallestDisplacementMeters)

您必须以米为单位设置位置更新之间的最小位移 默认为 0。

【讨论】:

  • 我也有同样的问题。 minimumDisplacementMeter 不工作。
【解决方案2】:

我通过设置修复了它

mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

而不是

mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

然后我设置

mLocationRequest.setInterval(UPDATE_INTERVAL); UPDATE_INTERVAL 为 10 * 1000 毫秒。

还有

mLocationRequest.setSmallestDisplacement(200) // 200 meters

【讨论】:

    猜你喜欢
    • 2016-09-18
    • 2023-03-07
    • 1970-01-01
    • 2017-05-14
    • 2015-10-10
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多