Fast Step-by-Step to Get Location

by Philip Barlow / from beautifuldecay.com

Configuration

Add Gradle dependency:

Add permission on AndroidManifest.xml

More about ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION.

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!

Deixe um comentário

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

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. 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 )

w

Conectando a %s