The Google Play Services library on Android includes the Location APIs including the Fused Location Provider, Activity recognition and Geofencing APIs.
The goal of the Fused Location Provider from the documentation:
- Simple APIs: Lets you specify high-level needs like "high accuracy" or "low power", instead of having to worry about location providers.
- Immediately available: Gives your apps immediate access to the best, most recent location.
- Power-efficiency: Minimizes your app's use of power. Based on all incoming location requests and available sensors, fused location provider chooses the most efficient way to meet those needs.
- Versatility: Meets a wide range of needs, from foreground uses that need highly accurate location to background uses that need periodic location updates with negligible power impact.
The use of the location APIs is documented in a training tutorial called Making Your App Location Aware. Unfortunately, with recent updates of the Google Play Services library (version 6.5.+ at the moment) the tutorial is now outdated and doesn't work. The tutorials use the LocationClient class. The class was first deprecated then removed in version 6.5. When the class was deprecated, in the documentation, Google recommended to use the FusedLocationProviderApi instead.
LocationClient removed in version 6.5 |
Receiving Location Updates
The high level approach for receiving location updates is:
- Request Location Permission
- Check for Google Play Services
- Define Location Services Callbacks
- Specify Update Parameters
- Start Location Updates
- Stop Location Updates
There are no differences between the LocationClient and FusedLocationProviderApi in steps 1, 2 and 4. The difference is in steps 3, 5 and 6.
Using the LocationClient
A striped down version of the code using the LocationClient looks like this:
Using the FusedLocationProviderApi
A striped down version of the code using the FusedLocationProviderApi looks like this:
Key differences
- ConnectionCallback interface from the GooglePlayServicesClient replaced with the ConnectionCallback interface from GoogleApiClient
- OnConnectionFailedListener interface from the GooglePlayServicesClient replaced with the OnConnectionFailedListener interface from GoogleApiClient
- Create a GoogleApiClient instead of LocationClient in onCreate
- In onStart and onStop use the GoogleApiClient instead of the LocationClient
- In onConnected use LocationServices.FusedLocationApi to request location updates
The switch from LocationServices to FusedLocationProviderApi shouldn't take more than a few minutes if you know what are you doing. Since Google are stubbornly refusing to document it I decided to write this blog post. I hope you find it useful.
Update: You can star this issue on the android bug tracker and maybe Google will fix the documentation.
Update: You can star this issue on the android bug tracker and maybe Google will fix the documentation.