I have set up the Google maps in my jetpack compose project. It's working fine i have set the intial mapType to MapType.HYBRID
. But there is a paramtere passed in the composable function called mapType
. I want to update the mapType of Google Map whenever the newvalue of paramtere is changed. Here is my code ->
@Composable
fun MapMeasurementCompactScreen(
mapType: MapType
) {
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(LatLng(1.35, 103.87), 10f)
}
val properties by remember {
mutableStateOf(
MapProperties(
isMyLocationEnabled = true,
mapType = MapType.NORMAL // here i want to use the parmater so it chnages the type of map dynamically
)
)
}
Box {
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = cameraPositionState,
properties = properties,
onMyLocationClick = {
cameraPositionState.position =
CameraPosition.fromLatLngZoom(LatLng(it.latitude, it.longitude), 10f)
}
) {
}
}
}
First, as you use the by
keyword, you can change val
to var
like this:
var properties by remember {
mutableStateOf(
MapProperties(
isMyLocationEnabled = true,
mapType = MapType.NORMAL
)
)
}
Then, when you want to change the properties, simply do
properties = properties.copy(mapType = MapType.TERRAIN) // set new MapType
Jetpack Compose will then pick up the changes and will recompose accordingly.