onLocationChanged() 被调用太多次,即使设备没有移动

onLocationChanged() is called too many times, even when the device hasn't move

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

@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;
}

编辑 mLocationRequest:

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;
}

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

setSmallestDisplacement(float smallestDisplacementMeters)

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

我通过设置

修复了它

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