Real-Time Location Tracking Using Google’s Fused Location Provider API

Imal Kumarage
The Startup
Published in
3 min readJul 9, 2020
Photo by henry perks on Unsplash

Location tracking and monitoring have seen a surge in modern application development with various services such as Uber heavily relying on location services that require consistency and accuracy. With businesses and corporations requiring location information and monitoring of their various assets, delivering comprehensive location solutions has been a tricky science for many developers.

Identifying the growing demand for a convenient location providing service Google introduced the Fused Location Provider API. This article will focus on using the API to track a user’s real-time location using an android application and through the example demonstrate the beauty of the API.

The API focuses on providing location information using a combination of a device’s GPS, Wi-Fi and internal sensors. The main selling point is that it decides in what combination to use these three resources based on the request parameters provided. In this context managing these resources is abstracted from the developer.

A simple use case to demonstrate the capacity of the API is tracking the movement of an individual walking towards a building, entering it and then leaving. In this scenario when the person is outdoors, the GPS service will function well. However, as he enters the building the GPS signal may drop. The Fused Location Provider handles the above scenario by tracking the location outdoors via GPS and when the GPS signal drops within the building, it utilizes Wi-Fi. During the switching process from GPS to Wi-Fi, to provide a smoother transition, the device’s internal sensors are used to predict the movement. In this manner, the management of available resources to provide reliable location services in a battery efficient manner is the true power of the API.

Integrating the API into your android application is a simple process. In this example, we will walk through the necessary steps required to build a simple location tracking application.

First, create an android project in Android Studio and within the application’s build.gradle file, ensure the following dependency is present:

dependencies{
implementation ‘com.google.android.gms:play-services:11.8.0’
}

Secondly, within the Manifest file you must provide either the Fine or Coarse location permission by adding the below snippet:

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

Now, create instances of FusedLocationProviderClient, LocationRequest and LocationCallBack in your activity.

private FusedLocationProviderClient fusedLocationClient;private LocationRequest locationRequest;
private LocationCallback locationCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}
}

The LocationRequest object is used to build a location request in accordance with parameters of our use case by providing details such as the update frequency, accuracy level and battery efficiency. Create a method to build the location request based on your desired parameters. An example is provided below:

private void buildLocationRequest() {locationRequest = new LocationRequest(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(100);locationRequest.setFastestInterval(100); locationRequest.setSmallestDisplacement(1);
}
//Build the location callback object and obtain the location results //as demonstrated below:private void buildLocationCallBack() {
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location: locationResult.getLocations()){
String latitude = String.valueOf(location.getLatitude());
String longitude = String.valueOf(location.getLongitude());
}
}
};
}

Once the locationRequest object and the locationCallBack objects are initialized, request location data by calling the FusedLocationProviderClient’s requestLocationUpdates method. Pass the locationRequest and locationCallback objects as parameters.

FusedLocationProviderClient.requestLocationUpdates
(
locationRequest,
locationCallback,
null
);

Based on the parameters defined by the request the API will continue to provide relevant location information that can be utilized to perform numerous functions such as location monitoring and real time speed tracking.

The Fused Location Provider API provides a convenient method to manage all underlying location technologies by abstracting their inner workings from the developer and providing high-level methods to specify the quality and accuracy of services required.

What is explored here is merely a fraction of what the API provides, and Google has provided comprehensive documentation that can be used to build diverse and robust applications in the future. Follow the link below to get started:

https://developers.google.com/location-context/fused-location-provider

--

--