Welcome to android location using google play services example. Today we will learn how to use Google Play services API to retrieve your mobile location with example app.

Android Location API Overview

In the previous tutorial, we retrieved the user’s location using Android’s Location API that was available since Android’s API 1. So why was there a need for introducing Google Play Location Services? Why hasn’t Google enhanced Android’s Location API? What are the advantages of Google Play Location Services over the default Android Location API? Let’s discuss these questions to get a clear idea.

Need for introducing Google Play Location Services

The Google Location Services API, part of Google Play Services, provides a more powerful, high-level framework that automates tasks such as location provider choice and power management. Furthermore, it provides new features such as user’s activity detection that wasn’t available in the Android Framework’s Location API. Currently, Google provides 5 user states which are In Vehicle, On Bicycle, On Foot, Still, and Tilting, which are good enough to detect user’s activity, and to provide right content according to user’s status.
Another feature it provides is Geofencing API that is used to notify a user entering or exiting a particular area.
The above advantages clearly indicate why Google Location Services API(also known as FusedLocationProviderApi) is Google’s recommended way of getting a user’s location. It provides the best accuracy based on our needs.

Why hasn’t Google enhanced Android’s Location API?

From a technical point of view, Google hasn’t improved Android’s Location API since Android has an independent update roll-out feature that lies in the hands of the smartphone manufacturer. Google has less control over it and hence decided to shift to a new API instead.

There are few important classes that are used to get the location:

  • LocationRequest : A data object that contains quality of service parameters for requests to the FusedLocationProviderApi. LocationRequest objects are used to request a quality of service for location updates from the FusedLocationProviderApi.
  • FusedLocationProviderApi : The main entry point for interacting with the fused location provider. The methods must be used in conjunction with a GoogleApiClient client which we’ll look into shortly.
  • com.google.android.gms.location.LocationListener : The LocationListener interface is used for receiving notifications from the FusedLocationProviderApi when the location has changed. The method onLocationChanged is invoked if the LocationListener has been registered with the location client using the requestLocationUpdates(GoogleApiClient, LocationRequest, LocationListener) or requestLocationUpdates(GoogleApiClient, LocationRequest, LocationListener, Looper) methods.

To use Google Play’s Location Services API we need to call GoogleAPIClient first.

GoogleAPIClient

GoogleAPIClient allows us to call multiple Google APIs using a single call.
Following is an example snippet to invoke the GoogleAPIClient with two APIs : Location Services and Drive API.

GoogleApiClient.ConnectionCallbacks and GoogleApiClient.OnConnectionFailedListener are implemented for addConnectionCallbacks and addOnConnectionFailedListener. onConnected() method that belongs to the GoogleApiClient.ConnectionCallbacks interface gets invoked when connections to all the APIs are established. onConnectionFailed() that belongs to GoogleApiClient.OnConnectionFailedListener interface is called in case of connection failure.

Project Structure

android-fused-location-api-project-structure

Code

Add the following dependency in the build.gradle file.

compile 'com.google.android.gms:play-services:9.8.0

Add the following permissions in the AndroidManifest.xml file.

The activity_main.xml is given below.

The MainAcitivity.java is given below.

mLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); is used to get the last known location from the location services.

Following is the output when the application is run on a genymotion emulator.

android-fused-location-api-output-1

Our emulator can’t fetch the location since Google Play services isn’t installed.
Let’s see what the output looks like on a smartphone.
android-fused-location-api-output-1
The Latitude and Longitude texts in the above application are updated every 5-15 seconds.

This brings an end to this tutorial. You can download the Android FusedLocationProvider Project from the link below.

By admin

Leave a Reply