Home > front end >  Google Maps CameraPosition Jetpack Compose
Google Maps CameraPosition Jetpack Compose

Time:12-13

When I use the CameraPositionState members isMoving and cameraMoveStartedReason, Google Maps freezes and stops responding. My goal is to do some work after the map is moved by gesture.

                val singapore = LatLng(1.35, 103.87)
                val cameraPositionState: CameraPositionState = rememberCameraPositionState {
                    position = CameraPosition.fromLatLngZoom(singapore, 11f)
                }

                if(cameraPositionState.isMoving &&
                    cameraPositionState.cameraMoveStartedReason == CameraMoveStartedReason.GESTURE) {
                    /* TODO */
                }

How can I do it better?

Thank you.

CodePudding user response:

Your map freezes because it recomposes while the map moves – which is a LOT btw, you can simply print it in your composable just to check that out. So you're doing some treatment at each recomposition.

You should rather treat it as a side effect instead. The simplest way would be with a LaunchedEffect:

LaunchedEffect(cameraPositionState.isMoving) {
    if (cameraPositionState.isMoving && cameraPositionState.cameraMoveStartedReason == CameraMoveStartedReason.GESTURE) {
        // Do your work here, it will be done only when the map starts moving from a drag gesture.
    }
}

Alternatively, you may want to have a look into derivedStateOf (also a side effect) that could better address that than a LaunchedEffect.

  • Related