Search code examples
androidgeolocationlistenergoogle-play-services

Stop location updates by using the last google play services api location


I'm building a simple weather application and I'm using the last google play services API to get the location. By calling the following:

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

we are able to get the location updates and thanks to the listener:

@Override
public void onLocationChanged(Location location)
{
    // update the location    
}

we can succeed in update the location. My issue is that I have a weather app and I should stop the location updates unregistering the listener:

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

I wouldn't have any button. One possible solution would be to set an higher possible interval or what do you say about it?


Solution

  • Inside your onLocationChanged(), update the location and then unregister the listener, like so :

    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);