Jetpack Compose:如何在 Google 地图中点击 Maker?

问题描述 投票:0回答:2

我使用officialGoogle Maps Compose lib,我无法理解...如何实现点击标记?

@Composable
fun MapContent(city: City)  {
    val pos = LatLng(city.latitude, city.longitude)
    val cameraPositionState = rememberCameraPositionState {
        position = CameraPosition.fromLatLngZoom(pos, 12f)
    }
    val mapState = rememberMarkerState(position = pos)
    
    GoogleMap(
        modifier = Modifier.fillMaxSize(),
        cameraPositionState = cameraPositionState,
        uiSettings = MapUiSettings(zoomControlsEnabled = false)
    ) {
        Marker(
            state = mapState,
            onClick = {
                Timber.tag("GoogleMapClick").d("click!!!")
                false
            }
        )
    }
}

这不起作用。你能帮我吗,出了什么问题吗?我想单击标记并显示包含城市信息的 BottomSheet,但看起来 onClick 不起作用...

implementation 'com.google.maps.android:maps-compose:2.5.3'
implementation 'com.google.android.gms:play-services-maps:18.1.0'
android google-maps-api-3 google-maps-markers google-maps-android-api-2 google-maps-api-2
2个回答
1
投票

您应该使用

onInfoWindowClick
代替
onClick

这是我有效的代码;)

            Card(modifier = Modifier
                .fillMaxWidth()
                .height(250.dp)
                .padding()
                .clip(RoundedCornerShape(24.dp))) {
                GoogleMap(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(250.dp)
                        .padding(),
                    cameraPositionState = cameraPositionState
                ) {
                    viewData.items.forEach {
                        val position = LatLng(
                            it.geolocalisation.first(),
                            it.geolocalisation.last())
                        Marker(
                            state = MarkerState(position = position),
                            title = it.nom,
                            snippet = it.categorie,
                            onInfoWindowClick = { _ ->
                                onClickItem(it)
                                coroutineScope.launch {
                                    if (sheetState.isVisible) sheetState.hide()
                                    else sheetState.show()
                                }
                            }
                        )
                    }
                }
            }

0
投票

只需使用

MarkerInfoWindow(
        state = MarkerState(position = singapore),
    ) {
        if (it.isInfoWindowShown) {
            //do something
            it.hideInfoWindow()
        }
© www.soinside.com 2019 - 2024. All rights reserved.