我使用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'
您应该使用
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()
}
}
)
}
}
}
只需使用
MarkerInfoWindow(
state = MarkerState(position = singapore),
) {
if (it.isInfoWindowShown) {
//do something
it.hideInfoWindow()
}