Getting location updates based on time interval or offset
I am using Fused Location Api to get location updates. When I set x seconds as the time interval, I got onLocationChanged () that gets called every x seconds. And when I set 10 meters at least the offset, then onLocationChanged () is not called until the user has moved 10 meters from their original position.
But I need to call onLocationChanged () when x seconds or 10 meters distance has passed.
any idea how i can achieve this.
My code
private Location mLastLocation;
public static final int REQUEST_LOCATION = 1006;
LocationRequest mLocationRequest;
private static final long POLLING_FREQ = 1000 * 10;
private static final long FASTEST_UPDATE_FREQ = 1000 * 10;
private static final long SMALLEST_DISPLACEMENT = 10;
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(POLLING_FREQ);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_FREQ);
mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT);
startLocationUpdates();
source to share
For offset only
mLocationRequest.setInterval(0);
mLocationRequest.setFastestInterval(0);
mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT);
For interval only
mLocationRequest.setInterval(POLLING_FREQ);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_FREQ);
mLocationRequest.setSmallestDisplacement(0); // Not needed, already default value is 0
Normal interval and distant parameters are calculated using AND. This means that when you change your position at least SMALLEST_DISPLACEMENT meter AND at least milliseconds POLLING_FREQ has passed, then it onLocationChanged()
will be fired.
source to share