android-maps-compose icon indicating copy to clipboard operation
android-maps-compose copied to clipboard

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

Open el-qq opened this issue 1 year ago • 1 comments

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@[email protected]:20)
    at com.google.maps.api.android.lib6.phoenix.dl.run(:com.google.android.gms.policy_maps_core_dynamite@[email protected]: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

el-qq avatar Jul 01 '24 13:07 el-qq

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?

kikoso avatar Jul 26 '24 06:07 kikoso

Closing this issue @el-qq , since it does not seem related to android-maps-compose.

kikoso avatar May 07 '25 06:05 kikoso