2015-02-03

In my previous post about Android GPS, Location Manager, I explained how to get device location (latitude & longitude) using the older android APIs. Now google introduced new way of getting device location using the Google Play Services.

A newer api called FusedLocationApi was introduced which connects with GoogleApiClient and gives us the best location available.

So let’s start this by creating a simple app.



DOWNLOAD CODE

VIDEO DEMO

1. Downloading & Importing Google Play Services

As this app needs Google Play Services, we need to setup the play services first. If you have the play services installed already, update them to latest version using Android SDK Manager.

1. Open Android SDK Manager and install or update the play services under Extras section.



2. In Eclipse goto File ⇒ Import ⇒ Android ⇒ Existing Android Code Into Workspace

3. Click on Browse and select Google Play Services project from your android sdk folder. You can locate play services library project from
android-sdk-windows\extras\google\google_play_services\libproject\google-play-services_lib

4. And check Copy projects into workspace option as shown in the below image, which places a copy of play services in eclipse workspace.



2. Creating Android Project

Once the play services are downloaded and imported into eclipse workspace, we can start building a simple app with the location services integrated.

1. In Eclipse create a new android project by navigating to File ⇒ New ⇒ Android Application Project and fill out all the required details.

I gave my project name as Location API and package name as info.androidhive.locationapi

2. Add the Google Play Services project as a library to our project. Right click on the project and select properties. In the properties window, on left side select Android. On the right, you can see a Add button under library section. Click it and select google play services library which we imported previously

3. Download this marker.png and paste it in your project’s src ⇒ res ⇒ drawable-ldpi folder. (Please note that this is a white color png image, it might not be visible in your browser window)

4. Open strings.xml located under res ⇒ values and add below string values.

5. Open colors.xml located under res ⇒ values and add below color values. If you don’t see colors.xml, create a new file with the name.

6. Open AndroidManifest.xml and add ACCESS_FINE_LOCATION permission. You also need to add below meta-data for google play services version.

After doing required changes, your AndroidManifest.xml should look like below.

7. Now we’ll quickly create a simple layout for our app. Open the layout file of your main activity (activity_main.xml) and add below code. This layout contains a TextView to display the location and two buttons (one is to get location and other is to toggle periodic location updates).

8. Now we’ll start adding the code related to location api. Open your main activity MainActivity.java and implement the class from ConnectionCallbacks, OnConnectionFailedListener.

In brief, you need to do below changes in your activity to get the user’s current location.

> First check for availability of Google Play Services by calling checkPlayServices() in onResume()

> Once play services are available on the device, build the GoogleApiClient by calling buildGoogleApiClient() method.

> Connect to google api client by calling mGoogleApiClient.connect() in onStart() method. By calling this, onConnectionFailed(), onConnected() and onConnectionSuspended() will be triggered depending upon the connection status.

> Once google api is successfully connected, displayLocation() should be called in onConnected() method to get the current location.

Add the below code to your main activity and run the project. Make sure that the wifi and location is enabled on your device before you test.

Receiving Location Updates

9. In certain scenarios, your app might needs location updates periodically. Let’s say you are building a direction app where user needs to be get updated whenever location is changed. In that case you need to request for location updates. Doing the below changes, you will get the new location wherever location is changed.

> Implement the activity from LocationListener which adds onLocationChanged() method.

> Create LocationRequest object by calling createLocationRequest() method in onCreate() method upon checking the play services availability.

> Add togglePeriodicLocationUpdates() method which toggles listening to location updates.

> Start the location updates by calling startLocationUpdates() in onConnected() and onResume() methods.

> Stop the location updates by calling stopLocationUpdates() in onStop().

> startLocationUpdates() and startLocationUpdates() methods are used to start/stop the location updates.

> onLocationChanged() method will be triggered whenever the location is changed. Calling displayLocation() inside onLocationChanged will display new location data on the UI.

After doing all the above changes, run and test the app. If your app is not getting location, follow below steps to debug the app.

3. Testing the App

Below are the few key points should be kept in mind while testing the app.

> Your device should have internet connection (Wifi or mobile 3G).

> Location service should be enabled. Go to Settings => Location => Turn On.

> When you run the app, if you are not able to get the location even though you have done above two steps, open any of google’s location apps (maps) and come back to our app or just tap on START LOCATION UPDATES.

> If you are testing the periodic location updates, go out and take a short walk (few steps). You should see the locationChanged method calling by giving latest location coordinates.

Complete Code:

Below is the complete code of MainActivity.java

References:
Making Your App Location-Aware

Show more