How to Use Different Types of Google Maps in Android using Jetpack Compose?
Last Updated :
26 Apr, 2025
When we use the default application of Google Maps we will get to see different types of Maps present inside this application. We will get to see satellite maps, terrain maps, and many more. We have seen adding Google Maps in the Android application. In this article, we will take a look at the implementation of different types of Google Maps in Android using Jetpack Compose.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Adding a new color to the Color.kt file
Navigate to app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it.
Kotlin
import androidx.compose.ui.graphics.Color
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
// on below line we are adding different colors.
val greenColor = Color(0xFF0F9D58)
Step 3: Adding Dependency to use Google Maps.
Navigate to build.gradle file and add the below dependency in the build.gradle file.
implementation("com.google.android.libraries.maps:maps:3.1.0-beta")
implementation("com.google.maps.android:maps-v3-ktx:2.2.0")
implementation("androidx.fragment:fragment:1.3.2")
After adding the dependency simply click on the sync now option to install it.
Step 4: Adding Permissions and Google Maps API key.
Navigate to app > AndroidManifest.xml file and add the below code in the application tag for adding the API key.
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="Enter your API key" />
Check out the below article on How to Generate API Key for Using Google Maps in Android?
Step 5: Create a New Layout File for Map Layout.
Navigate to app > res > Right click on it > New > Directory and name it as layout. Now right-click on that directory and click on New > Layout resource file. Create a new xml file and name it map_layout and add the below code to it. Comments are added in the code to get to know it in detail.
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Step 6: Create a new file for creating a Map
Navigate to app>java>your app's package name>Right click on it>New>Kotlin class and name it as MapUtils and add the below code to it. Comments are added in the code to get to know it in detail.
Kotlin
import android.os.Bundle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import com.google.android.libraries.maps.MapView
// on below line creating a new composable widget for displaying map
@Composable
fun rememberMapViewWithLifecycle(): MapView {
val context = LocalContext.current
// on below line initializing our maps view with id.
val mapView = remember {
MapView(context).apply {
id = R.id.map
}
}
// Makes MapView follow the lifecycle of this composable
val lifecycleObserver = rememberMapLifecycleObserver(mapView)
// on below line initializing lifecycle variable.
val lifecycle = LocalLifecycleOwner.current.lifecycle
// on below line adding observer for lifecycle.
DisposableEffect(lifecycle) {
lifecycle.addObserver(lifecycleObserver)
onDispose {
lifecycle.removeObserver(lifecycleObserver)
}
}
// returning map view on below line.
return mapView
}
@Composable
// creating a function for map lifecycle observer.
fun rememberMapLifecycleObserver(mapView: MapView): LifecycleEventObserver =
remember(mapView) {
// on below line adding different events for maps view
LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_CREATE -> mapView.onCreate(Bundle())
Lifecycle.Event.ON_START -> mapView.onStart()
Lifecycle.Event.ON_RESUME -> mapView.onResume()
Lifecycle.Event.ON_PAUSE -> mapView.onPause()
Lifecycle.Event.ON_STOP -> mapView.onStop()
Lifecycle.Event.ON_DESTROY -> mapView.onDestroy()
else -> throw IllegalStateException()
}
}
}
Step 7: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.example.newcanaryproject.ui.theme.NewCanaryProjectTheme
import com.example.newcanaryproject.ui.theme.greenColor
import com.google.android.libraries.maps.CameraUpdateFactory
import com.google.android.libraries.maps.GoogleMap
import com.google.android.libraries.maps.MapView
import com.google.android.libraries.maps.model.LatLng
import com.google.android.libraries.maps.model.MarkerOptions
import com.google.maps.android.ktx.awaitMap
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
var message = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NewCanaryProjectTheme {
// on below line we are specifying background color for our application
Surface(
// on below line we are specifying modifier and color for our app
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
) {
// on the below line we are specifying the theme as the scaffold.
Scaffold(
// in scaffold we are specifying the top bar.
topBar = {
// inside top bar we are specifying background color.
TopAppBar(backgroundColor = greenColor,
// along with that we are specifying title for our top bar.
title = {
// in the top bar we are specifying tile as a text
Text(
// on below line we are specifying text to display in top app bar.
text = "GFG",
// on below line we are specifying modifier to fill max width.
modifier = Modifier.fillMaxWidth(),
// on below line we are specifying text alignment.
textAlign = TextAlign.Center,
// on below line we are specifying color for our text.
color = Color.White
)
})
}) {
mapUI(LocalContext.current)
// on below line we are calling method to display UI
}
}
}
}
}
}
@Composable
fun mapUI(context: Context) {
val mapView = rememberMapViewWithLifecycle()
// on below line creating a column for our maps.
Column(
modifier = Modifier.fillMaxHeight().fillMaxWidth().background(Color.White)
) {
// on below line we are creating a row.
Row(
modifier = Modifier.fillMaxWidth().padding(all = 5.dp),
horizontalArrangement = Arrangement.Center
) {
// on below line adding a button.
Button(
onClick = {
// below line is to change the type of satellite map.
getMapLocation(mapView, "Hybrid")
},
// on below line adding a modifier for our button.
modifier = Modifier
.padding(5.dp)
.width(120.dp)
) {
// on below line adding a text for our button.
Text(text = "Hybrid", textAlign = TextAlign.Center)
}
// on below line adding a button.
Button(
onClick = {
// below line is to change the type of satellite map.
getMapLocation(mapView, "Terrain")
},
// on below line adding a modifier for our button.
modifier = Modifier.padding(5.dp).width(120.dp)
) {
// on below line adding a text for our button.
Text(text = "Terrain", textAlign = TextAlign.Center)
}
// on below line adding a button.
Button(
onClick = {
// below line is to change the type of satellite map.
getMapLocation(mapView, "Satellite")
},
// on below line adding a modifier for our button.
modifier = Modifier.padding(5.dp).width(120.dp)
) {
// on below line adding a text for our button.
Text(text = "Satellite", textAlign = TextAlign.Center)
}
}
// on below line adding a map view to it.
AndroidView({ mapView }) { mapView ->
// on below line launching our map view
CoroutineScope(Dispatchers.Main).launch {
val map = mapView.awaitMap()
// on below line adding zoom controls for map.
map.uiSettings.isZoomControlsEnabled = true
}
getMapLocation(mapView, "")
}
}
}
private fun getMapLocation(mapView: MapView, mapType: String) {
// below line is to create a list of address
// where we will store the list of all address.
// on below line we are getting our map view.
mapView.getMapAsync {
if (mapType.equals("Hybrid")) {
it.mapType = GoogleMap.MAP_TYPE_HYBRID
} else if (mapType.equals("Terrain")) {
it.mapType = GoogleMap.MAP_TYPE_TERRAIN
} else if (mapType.equals("Satellite")) {
it.mapType = GoogleMap.MAP_TYPE_SATELLITE
}
// on below line creating a lat lng for delhi location
val delhi = LatLng(28.644800, 77.216721)
// below line is use to add marker to each location of our array list.
it.addMarker(MarkerOptions().position(delhi).title("Marker at Delhi"))
// below line is use to zoom our camera on map.
it.animateCamera(CameraUpdateFactory.zoomTo(20.0f))
// below line is use to move our camera to the specific location.
it.moveCamera(CameraUpdateFactory.newLatLng(delhi))
}
}
Output:
Similar Reads
How to Use Different Types of Google Maps in Android?
When we use the default application of Google Maps we will get to see different types of Maps present inside this application. We will get to see satellite maps, terrain maps, and many more. We have seen adding Google Maps in the Android application. In this article, we will take a look at the imple
4 min read
How to Draw Polylines on Google Maps in Android using Jetpack Compose?
Google Maps is used in many Android applications. While using Google Maps there are many modifications that you will get to see while using Maps in this app. When we have used Google Maps in different apps such as OLA and Uber, we will see lines and routes drawn on our Maps. In this article, we will
6 min read
How to Draw Track on Google Maps in Android using Jetpack Compose?
In many android apps, we have seen that there is a route marker from a source location to the destination location. In this article, we will take a look at How we can draw a track on Google Maps in Android using Jetpack Compose. Step By Step ImplementationStep 1: Create a New Project in Android Stu
6 min read
Detect Different Types of Touch Gestures in Android using Jetpack Compose
Most of the smartphones of this generation have a touchscreen which serves as a medium for input as well as output. Users can click the screen for giving inputs to the device and the same screen is used for displaying the output for the given action. A user may touch different points on the screen a
3 min read
Android Jetpack Compose: How to Use Google Maps
Many applications such as Swiggy, Zomato, Ola, and others use Google Maps within their application to display the location within their application. Most of these applications use Google Maps to display the details within their application. In this article, we will take a look at How to integrate Go
4 min read
How to Add Custom Marker to Google Maps in Android using Jetpack Compose?
Many android applications use maps to display the location. We can get to see they use custom marker icons like a delivery boy image or an icon to display a marker on the map. In this article, we will take a look at How to use a custom icon as a marker on google maps in our android application. Ste
6 min read
How to Post Data to API using Retrofit in Android using Jetpack Compose?
APIs are used within Android Applications to interact with databases to perform various CRUD operations on data within the database such as adding new data, reading the existing data, and updating and deleting existing data. In this article, we will take a look at How to Post Data to API using Retro
5 min read
How to Add Multiple Markers to Google Maps in Android using Jetpack Compose?
Google Maps is used in most apps which are used to represent many locations and markers on Google Maps. We have seen markers on Google maps for multiple locations. In this article, we will take a look at the implementation of Multiple Markers on Google Maps in Android using Jetpack Compose. Step By
6 min read
How to Use Google Play Install Referrer API in Android using Jetpack Compose?
Google Play Install Referrer is the API that is used in most of the applications but it is not seen in the application. This functionality works under the hood and is used to check the sources from where the app is getting most of the downloads. Google Play Install Referrer API tells us that from wh
6 min read
How to Convert Pixel to DP in Android using Jetpack Compose?
We have seen the usage of many units while building android applications. These units are given different views such as image view, and text view to specify their height and width within the android application. Many times we also want to convert one unit to another unit. In this article, we will ta
5 min read