Getting GPS Location on Android with Fused Location Provider API
Smartphones and tablets are always more powerful and offering GPS Location to users is a common use case on Android Applications. By default, the Android SDK offers the Location API. However, this API is not really optimized to save your battery life.
So, Google has created the Fused Location Provider API integrated in the Google Play Services. It is a simple and battery-efficient location API for Android. In that tutorial, you are going to learn how to use this API to get GPS Location in your Android Apps.
To discover more information on the Fused Location Provider API, you can read the following documentation offered by Google :
Get location data for your app based on combined signals from the device sensors using a battery-efficient API.developers.google.com
In that tutorial, you are going to learn how to use the Fused Location Provider API to get the GPS Location on an Android Application.
Note that you can also watch this tutorial in video on YouTube :
Adding Fused Location Provider API dependency
Fused Location Provider API is part of the Google Play Services Location dependency. So, you need to add the following dependency in your build.gradle file :
Adding Permissions to the Android Manifest
The next step is to add the correct Permissions in the Android Manifest to request user’s locations. For that, you need to add ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions :
Creating the User Interface
Now, we are going to create a simple User Interface to display the location that will be returned by the Fused Location Provider API. We will have two TextView widgets :
- One TextView to display a “Current Location” label
- One TextView to display the user’s location got from the Fused Location Provider API
It gives us the following layout file :
Managing the Location Permissions
We target Android Oreo 8.1 (API Level 27), we need to manage Location Permissions. In the onCreate method of our Activity, we will request these permissions by calling the requestPermissions method :
To be sure to avoid to request permissions if the user has already granted them, we add the permissions to the list only if the permissions is not already granted :
In this code, it’s important to notice that we call the checkSelfPermission method to check if the user has already granted a specific permission.
The return of the requestPermissions method call is managed in a onRequestPermissionResult method callback. In this method, we check the request code to be sure to manage the correct call. It is important in an application in which several permissions are requested.
In the callback, we check if the user has granted the asked permissions or no. If yes, we can choose to connect our application to the GoogleApiClient instance. This part will be detailed later.
If no, we display a message to the user to alert him on the fact that, withouth the location’s permissions granted, the application couldn’t work properly.
It gives us the following code for the onRequestPermissionsResult callback :
Connecting to the GoogleApiClient
The Google Play Services are built around the GoogleApiClient object. So, in the onCreate method, we need to build GoogleApiClient instance by passing in parameter the API we want to use. Here, we choose to use the Location Services API.
We need also to implement the following interfaces to interact with the Google API Client : GoogleApiClient.ConnectionCallbacks and GoogleApiClient.OnConnectionFailedListener.
In the onStart method of our Activity, we connect the application to the GoogleApiClient by calling its connect method. And we don’t forget to disconnect from the client in the onPause method. Besides, we remove the location updates in the onPause method to save the battery of the user.
In the onResume method, we need to check if the user’s device has the Google Play Services installed. This check is made inside a checkPlayServices method :
Once the application is connected to the Google API Client, the onConnected callback is called. In this method, we could start to request location.
Requesting location from the API
Last step is to request user’s location from the Fused Location Provider API. In the onConnected method, we start by getting the last location of the user by calling the LocationServices.FusedLocationApi.getLastLocation method. If the location returned is not null, we display it to the user.
Then, we start to request for location updates in a startLocationUpdates dedicated method. In this method, we build a LocationRequest object detailing the priority, the refresh interval and the fastest interval for location updates.
After that, we implement the LocationListener interface. Like that we can call the LocationServices.FusedLocationApi.requestLocationUpdates method by passing in parameter the GoogleApiClient instance, the LocationRequest object built previously and our Activity as a LocationListener.
When the location changes, the onLocationChanged method is called. At this point, we have just to got the latitude and longitude contained in the Location object passed in parameter before displaying it to the user.
It gives us the following complete code for our MainActivity :
Testing our Application
Now, it’s time to test our Application getting user’s location thanks to the Fused Location Provider API.
Once the Android Applications is launched, you should have the following display on your device :
That’s all for that tutorial !
If you have some questions, feel free to use the comments below.
This channel will offer you tutorials on Android, Web Development and some others programming tips. Some tutorials and…www.youtube.com
If you want to discover some books to learn Java programming, I advice you to read the following article with my selection of the Top 6 Best Books for Java programming :