Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update location only if location changes

At the moment I use the next function to handle location changes

locationManager.requestLocationUpdates(bestProvider, (60000), 1000, (LocationListener) this); 

But the problem is that the minimum time (in miliseconds) the system will wait with checking if the location changed until the time has passed which is 60000 milliseconds in my case.

My question is if there is any way to make onLocation changed only trigger if the user moves a certain distance(in meters) without using any wait time?

Thanks.

like image 472
Dr. ali Avatar asked Mar 04 '26 14:03

Dr. ali


1 Answers

You can use the recommended FusedLocationProviderApi instead of LocationManager (it should be more accurate and offer better battery performance) and you can set some filters on the LocationRequest, you provide as a parameter to your location queries. Specifically you would be interested in this method:

setSmallestDisplacement()

that does what you want. Also check: setPriority(), setInterval(), setFastestInterval(), and others.

You can use them combined, like this:

private void startLocationUpdates() {
    final LocationRequest locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY).setInterval(LOCATION_UPDATE_INTERVAL)
        .setFastestInterval(LOCATION_UPDATE_FASTEST_INTERVAL).setSmallestDisplacement(LOCATION_UPDATE_SMALLEST_DISPLACEMENT_METERS);
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, locationListener);
}
like image 162
dud3rino Avatar answered Mar 06 '26 07:03

dud3rino