Search code examples
androidcoding-stylegoogle-maps-android-api-2

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


I have implemented OnLocationChanged(Location location) as a overrided method in my Activity as shown:

@Override
public void onLocationChanged(Location location) {

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

The problem is, this method get triggered even if the device is not moving. It always happens in an interval of milliseconds.

Debugger over location param gets me this logs:

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

...

EDIT:

This is the code where requestLocationUpdates is initialized:

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

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

Is the method triggered because of longitud\latitud change? Why is it triggering even when the device is not moving? Can I limit the accuracy? Is there any clean way to implement onLocationChanged()?


Solution

  • setSmallestDisplacement(float smallestDisplacementMeters)
    

    You have to set the minimum displacement between location updates in meters By default it is 0.