Android Location Based Services using the GoogleAPIClient and the FusedLocationProvider API -
In this series of three tutorials we will look at Android location based services and how to use the latest techniques to get the location of a device. In searching the web or other training resources, you may have noticed that the Location Manager is often used to get the location of a device. This techniques uses the Location Framework API and you can still use that. However, location based services are now part of the Google Play Services API and the Google developer’s website recommends that we use that approach.
The Location Framework API has been available from API level 1. The Google Play Services API was made available from API level 10 (android 2.3.3 or Gingerbread). The Google Play Services API offers better power management. The FusedLocationProvider API is part of the Google Play Services API and allows the Android device to automatically choose between what location sources to get the data. This can be GPS, WiFi, phone masts or even taking a location from another app.
Location based services consume battery power. Fundamentally, we are always trying to balance the power consumed against the frequency and accuracy of location updates. A highly accurate location requires the power hungry GPS. WiFi and phone masts consume less power but are also less accurate. Our example app can even consume no extra power and look to see if a location exists from another app. The resultant location taken from another app may be accurate, but can it be trusted!
Using the Google Play Services API obviously requires that Google Play Services are installed on the device. Using the Location Manager gives you the option of programming location data should this not be the case. In our example we will use the FusedLocationProvider API and the GoogleAPIClient that is part of the Google Play Services API.
Creating the Project
I chose to use Android Studio for this project. Start by creating a “new project”. If you are not sure how to do this then take a look at part 2 of my getting started series. I used the package name com.nbs.basiclocationservices. Follow through with the wizard and choose an “empty activity”.
Adding the Google Play Services Library Dependency
You first need to ensure that Google Play Services has been installed and is available within the development environment. Do this by checking you SDK manager. The screenshot below verifies that Google Play Services are installed.
You next need to add a library dependency within the project. This can be done one of two ways.
The first way is to select “File” and then “Project Structure” within Android Studio. In the resultant windows select the “app” on the left hand side and then select the dependencies tab.
Click the “Add” button and you will see the option for Google Play Services. Select this and then click OK. The project will resync.
The second way is to double click the “build.gradle (Module:app)” option to open up the text based gradle file and enter the following text under the dependencies section.
Note that your google play services version number will reflect the current version you have installed. Finally, if you use this method you have to “Sync Project with Grade Files” by clicking the button shown below:
Creating a Layout
I created basic layout comprising three TextViews. One TextView displays the title “My Location” and the other two will show the Latitude and Longitude of the device. The XML code the layout is shown below:
In part 2 we will create a framework for the java methods needed for our example.
After this tutorial your MainActivity.java file should look similar to the one below:
Your Android Manifest file should look similar to the one below:
Download Download Location Project Files