Javier Sivianes
Javier Sivianes

Reputation: 792

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()?

Upvotes: 4

Views: 2379

Answers (2)

Vishnu
Vishnu

Reputation: 1534

I fixed it by setting

mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

instead of

mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

Then I set

mLocationRequest.setInterval(UPDATE_INTERVAL); UPDATE_INTERVAL to 10 * 1000 milli seconds.

Also

mLocationRequest.setSmallestDisplacement(200) // 200 meters

Upvotes: 0

Ricardo Ruiz Romero
Ricardo Ruiz Romero

Reputation: 155

setSmallestDisplacement(float smallestDisplacementMeters)

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

Upvotes: 2

Related Questions