Search code examples
androidgeolocation

How to check if location available using FusedLocationProviderApi and GoogleClient?


We can check for location availability using Location manager's isProviderEnabled method. I am trying to use the new api's for location, i.e. fusedLocationProviderApi and trying to check if location can be retrieved. How to check that ?


Solution

  • As you've mentioned in your question, You can check if the Provider is enabled by using Location Manager's isProviderEnabled method, like below.

    LocationManager lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    

    By using FusedLocationApi with GoogleApiClient and LocationRequest, you can request Location Updates with LocationListener Interface which will have a callback method called when the location is changed. onLocationChanged(Location location). So you can know if the location can be granted or not in onLocationChanged method.

    So an example flow can be; Checking Location providers to see if they are enabled, and then trying to connect to GoogleApiClient to see if the user has GooglePlayServices etc that can get the User's Location.