googlemaps / android-maps-compose

Jetpack Compose composables for the Maps SDK for Android
https://developers.google.com/maps/documentation/android-sdk/maps-compose
Apache License 2.0
1.16k stars 142 forks source link

Crash MapEffect snapshot: Can't take a snapshot while executing in the background #590

Open el-qq opened 4 months ago

el-qq commented 4 months ago

Environment details

Library version and other environment information: 5.0.4

Steps to reproduce

  1. insert the code from the example
  2. swipe down and/or swipe up and down quickly in one place
  3. Crash happens

Code example


@OptIn(MapsComposeExperimentalApi::class)
@Composable
fun SnapshotBackground() {
    LazyColumn(
        modifier = Modifier.fillMaxSize(),
        contentPadding = PaddingValues(
            horizontal = 12.dp,
            vertical = 12.dp,
        )
    ) {
        items(250) {
            val mapBitmap = remember { mutableStateOf<Bitmap?>(null) }
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(300.dp)
                    .padding(bottom = 12.dp),
                contentAlignment = Alignment.Center,
            ) {
                if (mapBitmap.value == null) {
                    val isMapLoaded = remember { mutableStateOf(false) }
                    val cameraPositionState =
                        rememberCameraPositionState { position = CameraPosition.fromLatLngZoom(singapore, 16f) }

                    GoogleMap(
                        cameraPositionState = cameraPositionState,
                        uiSettings = MapUiSettings(
                            compassEnabled = false,
                            indoorLevelPickerEnabled = false,
                            mapToolbarEnabled = false,
                            myLocationButtonEnabled = false,
                            rotationGesturesEnabled = false,
                            scrollGesturesEnabled = false,
                            scrollGesturesEnabledDuringRotateOrZoom = false,
                            tiltGesturesEnabled = false,
                            zoomControlsEnabled = false,
                            zoomGesturesEnabled = false
                        ),
                        onMapLoaded = {
                            isMapLoaded.value = true
                        },
                    ) {
                        if (isMapLoaded.value) {
                            MapEffect { map ->
                                map.snapshot { snapshot ->
                                    mapBitmap.value = snapshot
                                }
                            }
                        }

                    }

                    Icon(
                        modifier = Modifier
                            .fillMaxSize()
                            .background(MaterialTheme.colors.surface),
                        painter = painterResource(id = android.R.drawable.star_on),
                        contentDescription = "Analytics section"
                    )

                } else {
                    Image(
                        bitmap = mapBitmap.value!!.asImageBitmap(),
                        contentDescription = null
                    )
                }
            }
        }
    }
}

Stack trace

 FATAL EXCEPTION: main
Process: com.google.maps.android.compose, PID: 9046
java.lang.IllegalStateException: Can't take a snapshot while executing in the background.
  at com.google.maps.api.android.lib6.impl.a.a(:com.google.android.gms.policy_maps_core_dynamite@241610205@241610202042.636179997.636179997:20)
    at com.google.maps.api.android.lib6.phoenix.dl.run(:com.google.android.gms.policy_maps_core_dynamite@241610205@241610202042.636179997.636179997:9)
     at android.os.Handler.handleCallback(Handler.java:959)
     at android.os.Handler.dispatchMessage(Handler.java:100)
      at android.os.Looper.loopOnce(Looper.java:232)
       at android.os.Looper.loop(Looper.java:317)
       at android.app.ActivityThread.main(ActivityThread.java:8501
kikoso commented 4 months ago

Hi @el-qq ,

This is a intended behavior on the subjacent Maps SDK when the map is not visible, see issue reported here.

Can you verify that your map is not visible on the screen?