Building a Live-tracking App using Google Maps on Android

Munachimso Ugorji
4 min readJan 10, 2022

--

This tutorial is a guide on how to build a live tracking app with google maps on Android.

This is a common feature in location tracking applications, this future has become a sort after feature in most apps.

To properly follow this tutorial, you need:

  • Android Studio
  • A good knowledge of Kotlin

CREATE A PROJECT

Ensure you already have Android Studio installed on your system. Start up Android Studio and create a new project, create a Google Maps Activity Project.

Give your new application a name and ensure you selected Kotlin as Language.

Click finish and allow your project to build.

GET GOOGLE MAPS API

in your recently created project, go to “AndroidManifest.xml”, you should see a todo, telling you to create an API Key, follow the link and go to

https://developers.google.com/maps/documentation/android-sdk/get-api-key

Under the “Creating API Keys” heading, click on “Go to the credentials page”

On the Credentials Page, click on “Create Project”

Give your new project a name and then create it

Click on the Maps SDK for Android Link and Enable it

After Enabling Maps SDK for Android, on the sidebar on the left, click on credentials

On the Credentials screen, click on Create Credentials, and then on API Key, An API key should be created for you. copy this key and let’s head back to the app.

Back at the app, open “AndroidManifest.xml” and replace the “YOUR_API_KEY” value with your api key

Select an Emulator and run the App.

The App should run on the emulator and show you a maps screen with location in Sydney Australia

In your AndroidManifest.xml file, define uses permission for location access, you can do this by add the code below on before the “<application” opening tag. Without these permissions, your application will fail at runtime when requesting location updates.

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

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

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

In your Apps build.gradle file, add this implementation, under dependencies, then sync your app to have the dependency imported

implementation 'com.google.android.gms:play-services-location:18.0.0'

Go back to your “MapsActivity.kt” and type in the code below, ensure you type them in and not copy so that the necessary imports are made.

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

private lateinit var mMap: GoogleMap
private lateinit var binding: ActivityMapsBinding
private lateinit var fusedLocationClient: FusedLocationProviderClient

@SuppressLint("MissingPermission")
private val locationPermissionRequest = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
val isGranted = permissions[Manifest.permission.ACCESS_FINE_LOCATION] ?: false
when {
isGranted -> fusedLocationClient.lastLocation
.addOnSuccessListener { location ->
renderMap(location)
}
else -> Snackbar.make(
binding.root,
"Location permission is needed to display the map",
Snackbar.LENGTH_LONG
).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMapsBinding.inflate(layoutInflater)
setContentView(binding.root)

locationPermissionRequest.launch(
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
)

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)

val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}

override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
}

private fun renderMap(location: Location) {
val user = LatLng(location.latitude, location.longitude)
mMap.addMarker(MarkerOptions().position(user).title("Me"))
mMap.moveCamera(CameraUpdateFactory.newLatLng(user))
mMap.animateCamera(
CameraUpdateFactory.newLatLngZoom(user, 15f)
)
}
}

Ensure that after typing you have all the following imports

import android.Manifest
import android.annotation.SuppressLint
import android.location.Location
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.example.livetrackingapplication.databinding.ActivityMapsBinding
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import com.google.android.material.snackbar.Snackbar

Then go ahead and run the app, you should have something like the screenshot shown below and as you move about the app would be updating your position

Finally if you’re using an emulator and your app is not running, ensure you have google map services enabled on your emulator.

Thanks

--

--