does any1 know how to specify initial camera posit...
# compose
m
does any1 know how to specify initial camera position for the map, based on a route? I can only do
position = CameraPosition.fromLatLngZoom
, which internally do
CameraUpdateFactory.newCameraPosition(value)
but i would like to use
CameraUpdateFactory.newLatLngBounds
using compose map library btw
m
If you are able to create the bounds before the map loads, you can pass a
cameraPositionState
into the GoogleMap composable and call
cameraPositionState.move(yourBounds)
inside
onMapLoaded
of the GoogleMap composable. If the bounds can change, you can call the same move function from where the new bounds get created
My rough code is:
Copy code
var isMapLoaded by remember { mutableStateOf(false) }
        val cameraPositionState = rememberCameraPositionState()
        val mapWidth: Int
        val mapHeight: Int
        with(LocalDensity.current) {
            mapWidth = maxWidth.roundToPx()
            mapHeight = maxHeight.roundToPx()
        }
        val bounds = remember(currentLocation, mapMarkers) {
            getCameraUpdateForBounds(currentLocation, mapMarkers, mapWidth, mapHeight)?.apply {
                if (isMapLoaded) {
                    cameraPositionState.move(this)
                }
            }
        }
        GoogleMap(
            modifier = modifier,
            cameraPositionState = cameraPositionState,
            onMapLoaded = {
                isMapLoaded = true
                bounds?.let {
                    cameraPositionState.move(it)
                }
            }
        )
m
For some reason, i tried that but it didnt move. I ended up using launchedEffect move inside the map content.