7

I am using LocationServices.FusedLocationApi to get location updates at a given interval, which is set on the LocationRequest object that is passed to requestLocationUpdates:

mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(30000);
LocationServices.FusedLocationApi.requestLocationUpdates(
            mGoogleApiClient, mLocationRequest, this);

I need to change the value of the interval after the listener started to receive location updates. What is the proper way to do that?

Pier-Luc Brault
  • 247
  • 2
  • 4

1 Answers1

7
    //remove location updates so that it resets
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

    //change the time of location updates
    mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(TIME_INTERVAL)
            .setFastestInterval(TIME_INTERVAL_MIN);

    //restart location updates with the new interval
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
mikebertiean
  • 3,611
  • 4
  • 20
  • 29
  • I am having problems with this, it seems that whenever I remove the location updates the location tracking just stops working altogether – Hoffmann Oct 19 '17 at 09:59
  • @hoffmann make sure you initialize the location request again and the fused location api. it should work. simple stop/start – mikebertiean Oct 19 '17 at 19:28