Google Maps in Jetpack Compose Android

Muhammad Danish
Geek Culture
Published in
2 min readMay 25, 2021


Integration of google maps is little bit difficult in jetpack compose as compare to android xml. But with out Xml we can’t integrate google maps in jetpack compose android app too.

In order to integrate google maps in jetpack compose you need to generate google map api key from google cloud console then you need to add below dependencies.


After that add meta data tag with your google map api key and internet permission in android manifest.xml file

<uses-permission android:name="android.permission.INTERNET"/><meta-data android:name=""

Now create a new directory under res directory and named it as layout and create a new resource file as i created layout_map.xml file and add below code in this file


Now you will be wondering how can we use this in jetpack compose so don’t worry I am going to show you that as well.

Now create a new file as i named it as MapUtils.kt and add below code in it

fun rememberMapViewWithLifecycle(): MapView {
val context = LocalContext.current
val mapView = remember {
MapView(context).apply {
id =

// Makes MapView follow the lifecycle of this composable
val lifecycleObserver = rememberMapLifecycleObserver(mapView)
val lifecycle = LocalLifecycleOwner.current.lifecycle
(lifecycle) {
onDispose {

return mapView

fun rememberMapLifecycleObserver(mapView: MapView): LifecycleEventObserver =
remember(mapView) {
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()

These are two composable functions which we are going to use. I copied these functions from you can have a look for more information.

After that you need to write below in your activity composable function

val mapView = rememberMapViewWithLifecycle()

We can use Xml layout files using AndroidView() in composable functions

modifier = Modifier
) {
AndroidView({ mapView}) {mapView->
CoroutineScope(Dispatchers.Main).launch {
val map = mapView.awaitMap()
map.uiSettings.isZoomControlsEnabled = true

val pickUp = LatLng(-35.016, 143.321)
val destination = LatLng(-32.491, 147.309)
val markerOptions = MarkerOptions()
.title("Sydney Opera House")

val markerOptionsDestination = MarkerOptions()
.title("Restaurant Hubert")

map.addPolyline(PolylineOptions().add( pickUp,
LatLng(-34.747, 145.592),
LatLng(-34.364, 147.891),
LatLng(-33.501, 150.217),
LatLng(-32.306, 149.248),



I have intialized map in Coroutine scope and draw two markers on map and then i draw polyline for routine showing.

Finally the output :

If you face any issue then please comment so that i can help you.