How to track real time location in Android with Google Maps

Realtime location tracking is the backbone of all delivery and tracking apps in the market. For example tracking your food on your favourite delivery app or tracking your cab on apps like Uber. The concept behind tracking realtime location is to get periodic location updates, track it and and broadcast it to all the clients watching for location updates in the map.

In this tutorial we will see how to build a realtime tracking app using Android Location API, Google Maps API and Firebase Realtime Database. We will track user’s realtime location with Android’s Location API and push the location update via Firebase RTDB. We will stream the location updates on Google Maps using Google Maps API. We will create two apps in this tutorial. One app for the delivery client and other app for the user tracking location.

Delivery Location App

Create a new Firebase project and setup no sql Firebase realtime database. If you are new to Firebase then I suggest you to go through this post and create your first realtime database project. Firebase RTDB will be used to synchronize location updates among all the devices. The target device will update its location to database and the same will be read realtime by the end users. Thus creating a realtime synchronization between the devices.

Steps to create Android app:

  1. Create a new firebase app
  2. Connect Android project to Firebase
  3. Create realtime database instance in Firebase and update security rules.
  4. Add Location API, Firebase database dependencies in app’s gradle file
  5. Add Internet and Location permissions in AndroidManifest.xml

You can check my last post on how to ask user permission for location access and use location api to track location. In this tutorial we will create a service that will constantly tracks any change in the location and pushes the location to firebase database. Lets create our service that will get location updates.

AndroidManifest.xml

Create a new Service and name it LocationTrackerService

In onStartCommand we request fused location provider to provide any update in location. We register a callback with requestLocationUpdates method to get location data. Once we receive location data we push the location to a predefined path in database.

ref.setValue(lastLocation) 

Create layout file for our activity class. activity_main.xml

In our activity I have added a button to start tracking location data.

Google has done many changes in Android O to save battery and optimize battery consumption. One of the major change is limiting the background execution of services. Any service that do not have persistent notification is killed by the system as soon as the activity that started it is stopped. In order to run our service post API 26 (Android O, N) we need to bind the service with a persistent notification to the user.

Create a new Java class and name it LocationNotification. In this class we will also learn how to create notification for Android O using notification channel.

Now you are ready to start tracking device and the location will be updated in database. Any device that want to track it can read database data changes and mark it in Google Maps. Let’s create our tracking app now.

Tracking APP

Create a new project and select Google Maps Activity in project selection.

create android studio new project for google maps

From Tools -> Firebase assistant connect this new Android project with the save Firebase project that was used for previous project. We need to have same firebase project synced in both the apps.

Add Firebase database dependency and create Google maps for Android api key from Google API Console. Select firebase project and enable api. Replace API key in google_maps_api.xml

Update MapsActivity with the following code. We will add a listener (addValueEventListener) to read any changes from realtime database. updateMap method draws a polyline to display the path taken by the device.

 

 

 

Leave a Reply

%d bloggers like this: