Add Gradle dependency:
Add permission on AndroidManifest.xml
Using the GoogleApiClient
At first, we need to instanciate the GoogleApiClient, because throught this instance we can access any service from Google Services, like the Location Service. It is a client for ALL services of the Google Service API (Location, ActivityRecognition and etc..).
This Activity below shows how it happens:
When the connection with the API is done, we can use the services of the API.
Adding the Location Service API
It’s easy, just add the API reference during the GoogleApiClient instanciation:
Now, when the Google API Client get ready to be used, the Location Service will be ready too and we can call methods of LocationServices.FusedLocationApi:
Getting Last Location
Throught the LocationServices.FusedLocationApi we can get the last location:
Requesting Location Updates
If we want to get the location everytime it changes, we can set an listener to receive the current location instance:
Instead of use the getLastLocation() we gonna use the requestLocationUpdates(). We have to set the updates configuration (time interval, min displacement in meter and etc) through an instance of com.google.android.gms.location.LocationRequest and we have to set the listener that will receive the updates.
Ops! Don’t forget to remove the listener when you close the connection with the Google API. We gonna do it using the method removeLocationUpdates of the class LocationServices.FusedLocationApi.
Now that you have an overview, you can read more about each step sink!