avatarDaniel Atitienei

Summary

This article provides a step-by-step guide on how to integrate Google Maps into an Android app using Jetpack Compose.

Abstract

The article begins by explaining the importance of obtaining a Google Maps API key from the Google Cloud Console to authenticate and enable the app to access the Google Maps API. It then provides instructions on how to add the Secrets Gradle Plugin into the project and the necessary dependencies to the build.gradle file. The article also explains how to add the Google Maps API key to the local.properties file and the AndroidManifest.xml file.

The article then moves on to demonstrate how to display the map, add a marker, polygon, circle, and polyline, and enable Street View. It also explains how to style the map using the MapUiSettings and create a new theme using the Google Maps Styling Wizard.

Bullet points

  • Get a Google Maps API key from the Google Cloud Console
  • Add the Secrets Gradle Plugin into the project build.gradle file
  • Add the necessary dependencies to the build.gradle file
  • Add the Google Maps API key to the local.properties file
  • Add the key variable to the AndroidManifest.xml file
  • Display the map using the GoogleMap composable
  • Add a marker using the Marker composable
  • Add a polygon using the Polygon composable
  • Add a circle using the Circle composable
  • Draw a polyline using the Polyline composable
  • Enable Street View using the StreetView composable
  • Style the map using the MapUiSettings
  • Create a new theme using the Google Maps Styling Wizard

How to Integrate Google Maps into Your Android App using Jetpack Compose

In this article, we’ll integrate Google Maps into an Android app using Jetpack Compose. We’ll create a map and we’ll modify it to meet your needs. By the end of this article, you’ll have a firm grasp on how to integrate Google Maps into your app, giving your users a rich and interesting experience.

Quick actions

  1. Display the Map
  2. Add a Marker
  3. Add a Polygon
  4. Add a Circle
  5. Draw Polyline
  6. StreetView
  7. Map Styling

Setup

Step 1

Get a Google Maps API key: To use Google Maps in your Android app, you will need to obtain an API key from the Google Cloud Console. This key will be used to authenticate your app and enable it to access the Google Maps API.

Step 2

Add the Secrets Gradle Plugin into the project build.gradle: This plugin secures our Google Maps API key.

Step 3

Add these dependencies and then sync the project: Google Maps API requires the Google Play Services library to be installed on the device, so you’ll need to add the library to your app. We also need the Secrets Gradle Plugin to secure our API key.

Step 4

Add the Google Maps API key in local.properties : Open the local.properties in your project-level directory, and then add the following code. Replace YOUR_API_KEY with your API key.

Step 5

Add the key variable in AndroidManifest : Open the AndroidManifest file and then add the meta-data below the </activity .

I know we had a lot of steps until now, but we’re finally done with the setup.

Display the map

Let’s start by creating a point and then create a camera that will zoom in on that point. To display the map we just have to use the GoogleMap composable.

Add a Marker

You can add a marker on a specific location using the Marker composable.

Add a Polygon

You can add a polygon on a specific location using the Polygon composable.

Add a Circle

You can add a circle on a specific location using the Circle composable.

Draw a Polyline

You can draw a polyline using the Polyline composable.

Street View

You can add a Street View given a location using the StreetView composable.

Map Styling

With the MapUiSettings we enable/disable gestures and buttons.

Let’s add a new theme to our map. This website helps us to create different themes and then we can use them in our code. After you created your theme, copy the JSON and then create an object we’ll use later.

Now we can add this style into the MapProperties .

I hope this article helped in your development journey. Remember to stay updated on my latest content by following me and subscribing to the newsletter. Thank you for reading!

If you like my content and want to support me, I would appreciate a coffee!

Jetpack Compose
Jetpack
AndroidDev
Android App Development
Kotlin
Recommended from ReadMedium