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

android - MarkerInfoWindow not recomposing - Stack Overflow

programmeradmin2浏览0评论

I'm trying to update the InfoWindowMarker content of my Google map in Compose, for that, I added a onClick to the InfoWindowMarker and when it's clicked, it changes a "favorite" variable which is hoisted on the map. That favorite variable is passed to the marker, and depending of that variable shows or not another Text.

The problem is that when the marker is pressed, the variable is changed (I tested it under debug mode with a breakpoint) but the info window is not changed! the favorite Text doesn't appear, it's like it's not being recomposed.

This is my Google map:

GoogleMap(
    modifier = Modifier.fillMaxSize(),
    cameraPositionState = cameraPositionState
) {
    val bitmapDescriptor: BitmapDescriptor by remember { mutableStateOf(BitmapDescriptorFactory.fromResource(R.drawable.place)) }

    for (busStop in uiState.data) {
        var favorite by remember { mutableStateOf(false) }

        val markerState = rememberMarkerState(position = LatLng(busStop.lat, busStop.lon))
        CustomMarker(
            favorite = favorite,
            busStop = busStop,
            markerState = markerState,
            bitmapDescriptor = bitmapDescriptor,
            showMarker = showMarkers,
            onMarkerClicked = { favorite = !favorite }
        )
    }
}

This is my CustomMarker:

@Composable
fun CustomMarker(
    favorite: Boolean,
    busStop: BusStop,
    markerState: MarkerState,
    bitmapDescriptor: BitmapDescriptor,
    showMarker: Boolean,
    onMarkerClicked: () -> Unit,
    modifier: Modifier = Modifier
) {
    MarkerInfoWindowContent(
        state = markerState,
        icon = bitmapDescriptor,
        visible = (showMarker),
        onInfoWindowClick = {
            onMarkerClicked()
        }
    ) {
        Column(
            modifier = modifier.wrapContentSize().padding(8.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(text = busStop.id.toString(), color = Color.Red)
            Text(text = busStop.name, color = Color.Red)

            if (favorite)
                Text("favorite"}
        }
    }
}
发布评论

评论列表(0)

  1. 暂无评论