Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Location Updates based on time interval or Displacement

I am using Fused Location Api to get location updates. When I set x seconds as time interval then I got onLocationChanged() called after every x seconds. And when I set 10 meter as minimumDisplacement then onLocationChanged() is not called until user moves 10 meter from its original position.

But I need to have onLocationChanged() called when either x seconds passed or 10 meter distance covered.

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();
like image 628
Karan Nagpal Avatar asked Dec 23 '22 16:12

Karan Nagpal


1 Answers

Only for displacement

  mLocationRequest.setInterval(0);
  mLocationRequest.setFastestInterval(0);
  mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT);

Only for interval

 mLocationRequest.setInterval(POLLING_FREQ);
 mLocationRequest.setFastestInterval(FASTEST_UPDATE_FREQ);
 mLocationRequest.setSmallestDisplacement(0); // Not needed, already default value is 0

Normally interval and distance params are being calculated with AND. It means when you change your position at least SMALLEST_DISPLACEMENT meter AND at least POLLING_FREQ milliseconds have passed, then onLocationChanged() will be fired.

like image 121
blackkara Avatar answered Jan 11 '23 23:01

blackkara