RH201
RH201

Reputation: 331

Android - Get a GPS Update based on specific amount of Time AND a specific distance travelled

I want to get location updates every 60 seconds OR every 1500 meters traveled. Obviously, the LocationManager allows you to set a minimum for time and meters, but the GPSUpdate will not trigger until both the time and distance minimums are met. But what i want is to have the GPS Update every 60 seconds (no matter how far the user has traveled) and every 1500 meters (no matter how long it took).

I have tried using multiple locationClients with the locationRequests set to each criteria, but they both cannot connect at the same time.

Is there any simple way to achieve this?

Upvotes: 3

Views: 895

Answers (1)

RH201
RH201

Reputation: 331

Apologies for never posting back, i had forgotten all about this question.

Whatever my solution was in the past would not have been correct as i recently added a more efficient way to do this.

First.. if you are using FusedLocationAPI and GoogleApiClient, then see my answer posted here.

If you are using the LocationManager class and not the GoogleApiClient, then the solution is similar.

You need to create two requestLocationUpdates, one for Time only, and one for Distance only, with the unused field set to 0, for example:

locationManager.requestLocationUpdates(bestProvider, 0, UPDATE_MIN_DISTNACE, myBackupLocationDistanceIntervalListener);
locationManager.requestLocationUpdates(bestProvider, UPDATE_TIME_INTERVAL, 0, myBackupLocationTimeIntervalListener);

This can also be done with Criteria if need be:

locationManager.requestLocationUpdates(0, UPDATE_MIN_DISTNACE, gpsCriteria, myBackupLocationDistanceIntervalListener, Looper.getMainLooper());
locationManager.requestLocationUpdates(UPDATE_TIME_INTERVAL, 0, gpsCriteria, myBackupLocationTimeIntervalListener, Looper.getMainLooper());

Then just define your listeners. You can use either the same or separate listeners to handle the results from the requests.

Hope this helps.

Upvotes: 3

Related Questions