Home » Android » Android Location API using Google Play Services Example

About Chryssa Aliferi

Avatar photo
Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.

Android Location API using Google Play Services Example

The great power of mobile applications software, is that it gives us the opportunity to develop many ideas that use hardware features, in order to complete simple everyday tasks. One of the unique features of mobile applications is the location awareness. Mobile users take their devices with them everywhere, and adding location awareness to your app may offer a new road of endless possibilities.

In Android, Google provides via Google Play Services an API for automated location tracking of the position of the user and the mobile device. This is the Fused Location Provider API.

In this example, we are going to show a basic Fused Location API implementation, by developing a simple Android app that will automatically start the location provider and track the current position on the device. In this tutorial we are going to make a simple implementation, however for a more detailed documentation you can visit the Google APIs for Android.

Let’s see how we are going to implement it.

Want to create a kick-ass Android App?

Subscribe to our newsletter and download the Android Programming Cookbook right now!

With this book, you will delve into the fundamentals of Android programming. You will understand user input, views and layouts. Furthermore, you will learn how to communicate over Bluetooth and also leverage Google Maps into your application!
For our example will use the following tools in a Windows 64-bit or an OS X platform:

  • JDK 1.7
  • Android Studio 1.3.2
  • Android SDK 6.0

Let’s take a closer look:

1. Create a New Android Studio Project

You may skip project creation and jump directly to the beginning of the example below.

Open Android Studio and choose “Start a new Android Studio Project” in the welcome screen.

“Welcome to Android Studio” screen. Choose “Start a new Android Studio Project”.

“Welcome to Android Studio” screen. Choose “Start a new Android Studio Project”.

Specify the name of the application, the project and the package.

“Configure your new project” screen. Add your application name and the projects package name.

“Configure your new project” screen. Add your application name and the projects package name.

In the next window, select the form factors your app will run on.

“Target Android Devices” screen.

“Target Android Devices” screen.

In the next window you should choose “Add no activity”. In this example, we are going to create our Activity. So, select the “Blank Activity” option and press Next.

“Add an activity to Mobile”. Choose: “Add no activity”.

“Add an activity to Mobile”. Choose: “Add no activity”.

Now, our project has just been created!

2. Create the layout of the main AndroidLocationActivity

We are going to make the layout of the AndroidLocationActivity, which will be a simple linear layout xml with vertical orientation in order to help us display the latitude and longitude of the current position of the mobile device.

Add a new xml file inside /res/layout folder, with name activity_main.xml. If the layout folder does not exist, you should create a new resource directory with this name. We should have the /res/layout/activity_main.xml file and paste the code below.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:text="Get Location Latitude and Longitude"
        android:textSize="25dp" />

        android:src="@drawable/location" />

        android:textSize="22dp" />

        android:textSize="22dp" />


3. Create a New Android Studio Project

Add a new Java class inside src/com.javacodegeeks.examples.androidLocationAPIExample/ so that we are going to have the src/com.javacodegeeks.examples.androidLocationAPIExample/AndroidLocationActivity.java file and paste the code below.


package com.javacodegeeks.examples.androidLocationAPIExample;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

public class AndroidLocationActivity extends Activity implements LocationListener {

    // Google client to interact with Google API
    private GoogleApiClient mGoogleApiClient;

    private LocationRequest mLocationRequest;
    private TextView latitude, longitude;

    private double fusedLatitude = 0.0;
    private  double fusedLongitude = 0.0;

    public void onCreate(Bundle savedInstanceState) {

        if (checkPlayServices()) {


    private void initializeViews() {
        latitude = (TextView) findViewById(R.id.textview_latitude);
        longitude = (TextView) findViewById(R.id.textview_longitude);

    protected void onStop() {

    // check if google play services is installed on the device
    private boolean checkPlayServices() {
        int resultCode = GooglePlayServicesUtil
        if (resultCode != ConnectionResult.SUCCESS) {
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
                        "This device is supported. Please download google play services", Toast.LENGTH_LONG)
            } else {
                        "This device is not supported.", Toast.LENGTH_LONG)
            return false;
        return true;

    public void startFusedLocation() {
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API)
                    .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                        public void onConnectionSuspended(int cause) {

                        public void onConnected(Bundle connectionHint) {

                    }).addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {

                        public void onConnectionFailed(ConnectionResult result) {

        } else {

    public void stopFusedLocation() {
        if (mGoogleApiClient != null) {

    public void registerRequestUpdate(final LocationListener listener) {
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(1000); // every second

        new Handler().postDelayed(new Runnable() {
            public void run() {
                try {
                    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, listener);
                } catch (SecurityException e) {
                } catch (Exception e) {
                    if (!isGoogleApiClientConnected()) {
        }, 1000);

    public boolean isGoogleApiClientConnected() {
        return mGoogleApiClient != null && mGoogleApiClient.isConnected();

    public void onLocationChanged(Location location) {

        Toast.makeText(getApplicationContext(), "NEW LOCATION RECEIVED", Toast.LENGTH_LONG).show();

        latitude.setText(getString(R.string.latitude_string) +" "+ getFusedLatitude());
        longitude.setText(getString(R.string.longitude_string) +" "+ getFusedLongitude());

    public void setFusedLatitude(double lat) {
        fusedLatitude = lat;

    public void setFusedLongitude(double lon) {
        fusedLongitude = lon;

    public double getFusedLatitude() {
        return fusedLatitude;

    public double getFusedLongitude() {
        return fusedLongitude;

Let’s see in detail the code above.

With the checkPlayServices() method, we can check if our device has installed the Google Play Services application in each device, so that we can make use of the Google APIs’ that Google Play Services provide.

With the startFusedLocation() method, we build the GoogleApiClient by calling the mGoogleApiClient.connect() method and implementing what our client should do onConnectionFailed(), onConnected() and onConnectionSuspended().

After our GoogleApiClient is connected we call the registerRequestUpdate(final LocationListener listener). With this method we register our Activity to Location changes every one second, with PRIORITY_HIGH_ACCURACY.

By implementing the LocationListener we must implement the onLocationChanged(Location location) method. With this method we “catch” every location change, through the Location object.

Finally, when we stop the Activity we should unregister the GoogleApiClient by calling the stopFusedLocation() method.

4. Android Manifest

The AndroidManifest.xml of our project is simple and contains the main Activity of our example:


<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


In this AndroidManifest.xml we have to add the android.permission.ACCESS_FINE_LOCATION permission in order for our app to grant the permission of using the fine location feature. This happens at line 4.

5. Composing build.gradle file

We should add the Google Play Services library in order for our app to implement the Google Fused Location API. We can add them as dependencies to our application via build.gradle file.


apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.0'

    defaultConfig {
        applicationId "com.javacodegeeks.examples.androidLocationAPIExample"
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile "com.google.android.gms:play-services:8.3.0"

With the line compile 'com.google.android.gms:play-services:8.3.0' we add in our Gradle configuration the public repository in which Google supports Google Play Services library with Gradle. In this way we have added Google Play Services library in our project, so that we can use the Google Fused Location API.

6. Build, compile and run

When we build, compile and run our project, the main AndroidLocationActivity should look like this:

This is the AndroidLocationAPI example.

This is the AndroidLocationAPI example.

7. Download the Android Studio Project

This was an example of Android AndroidLocationActivity.

You can download the full source code of this example here: AndroidLocationAPIExample.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments