最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Deal with Maps in ListDetail - Stack Overflow

programmeradmin1浏览0评论

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)
                )
            }
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论