Reputation: 37731
I have a list, and when user selects a element of the list, the detail is a Google map with some markers of that selected item.
If I display it in portrait, everything works, but if I display it in landscape, the list and the detail is visible at once, and then only works correctly one time.
When I select for the first time an item, the map is displayed, and the animation for center the camera works perfectly (I have a launched effect that centers it after markers are loaded). But when I select another item, then, the map doesn't center the camera and the old markers are not deleted, instead of that, the newer ones are painted on the map, and the camera doesn't move.
How to solve this?
My scaffold:
ListDetailPaneScaffold(
modifier = Modifier.fillMaxSize(),
directive = navigator.scaffoldDirective,
value = navigator.scaffoldValue,
listPane = {
AnimatedPane {
LinesList(
lines = uiState.lines,
onItemClick = { index ->
navigator.navigateTo(ListDetailPaneScaffoldRole.Detail, index)
},
)
}
},
detailPane = {
AnimatedPane {
navigator.currentDestination?.content?.let { index ->
vm.selectLine(uiState.lines[index])
MapPanel(mapState = uiState.mapState)
}
}
},
)
My map:
@Composable
fun MapPanel(
mapState: MapState,
modifier: Modifier = Modifier
) {
val showMarkers: Boolean by remember { derivedStateOf { cameraPositionState.position.zoom > minZoomForVisibleMarkers } } // default 15
val boundsBuilder = LatLngBounds.builder()
GoogleMap(
modifier = modifier.fillMaxSize(),
cameraPositionState = cameraPositionState
) {
val bitmapDescriptor: BitmapDescriptor by remember { mutableStateOf(BitmapDescriptorFactory.fromResource(R.drawable.place)) }
for (busStop in mapState.busStops) {
val markerState = rememberMarkerState(
position = LatLng(busStop.lat, busStop.lon)
)
Marker(
state = markerState,
icon = bitmapDescriptor,
visible = showMarkers,
onClick = {
selectBusStop(busStop)
false
}
)
boundsBuilder.include(markerState.position)
}
if (centerInMarkers && mapState.busStops.isNotEmpty()) {
LaunchedEffect(key1 = true ) {
cameraPositionState.animate(
update = CameraUpdateFactory.newLatLngBounds(bounds, 100)
)
}
}
}
}
Upvotes: 0
Views: 46