https://kotlinlang.org logo
#compose
Title
# compose
m

myanmarking

03/29/2022, 5:41 PM
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

Mike

03/29/2022, 9:08 PM
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

myanmarking

03/29/2022, 10:03 PM
For some reason, i tried that but it didnt move. I ended up using launchedEffect move inside the map content.
5 Views