android Jetpack组成:如何在谷歌Map中点击创客?

xu3bshqb  于 2023-01-07  发布在  Android
关注(0)|答案(1)|浏览(122)

我使用official谷歌Map合成库,我不明白...如何实现点击标记?

@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'
6yjfywim

6yjfywim1#

您应该使用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()
                                }
                            }
                        )
                    }
                }
            }

相关问题