Fast Step-by-Step to Get Location

by Philip Barlow / from


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 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!

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do

Você está comentando utilizando sua conta Sair /  Alterar )

Foto do Google

Você está comentando utilizando sua conta Google. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s