我已经成功地在Google地图上添加了这个标记:
Marker(
state = MarkerState(
position = LatLng(lat, lng)
),
title = "Party",
snippet = "Come to party.",
draggable = true
)
设置为可拖动。现在我想知道什么时候有人在拖拽它。我发现这个函数称为InputHandler(),但我不知道如何使用它。
我试着使用:
InputHandler(
onMarkerDragEnd = { marker ->
Log.d(TAG, "${marker.id}")
}
)
但是什么也没发生。有人能帮忙吗?或者其他知道标记何时被拖拽的方法吗?
你可以使用OnMarkerDragListener接口来监听标记拖动事件。
val marker = googleMap.addMarker(
MarkerOptions()
.position(LatLng(lat, lng))
.title("Location")
.snippet("set Location.")
.draggable(true)
)
googleMap.setOnMarkerDragListener(object : OnMarkerDragListener {
override fun onMarkerDragStart(marker: Marker?) {
// Called when marker drag starts
}
override fun onMarkerDrag(marker: Marker?) {
// Called while marker is being dragged
}
override fun onMarkerDragEnd(marker: Marker?) {
// Called when marker drag ends
Log.d(TAG, "${marker?.id}")
}
})
Formap -compose图书馆
var markerPosition by remember { mutableStateOf(LatLng(37.7749, -122.4194)) }
var isDragging by remember { mutableStateOf(false) }
GoogleMap(
modifier = Modifier.fillMaxSize(),
initialCameraPosition = CameraPosition(target = markerPosition, zoom = 10f),
onMapClick = { /* handle map clicks */ }
) { map ->
if (isDragging) {
// While dragging, create a temporary marker with opacity set to 0
Marker(
position = markerPosition,
alpha = 0f
)
} else {
// When not dragging, create a visible marker
Marker(
position = markerPosition,
title = "Party",
snippet = "Come to party.",
draggable = true,
onClick = { /* handle marker clicks */ }
)
}
}
// Listen for drag events using the pointer input system
PointerInput(Unit) {
forEachGesture {
awaitPointerEventScope {
val down = awaitFirstDown()
if (down.changes.any { it.changedToDown() && it.position in markerPosition.toOffset() }) {
// If the user starts a drag gesture on the marker, set isDragging to true
isDragging = true
}
val up = awaitUpOrCancellation()
if (up.changes.any { it.changedToUp() }) {
// If the user stops a drag gesture, set isDragging to false and update the marker position
isDragging = false
markerPosition = up.changes.first { it.changedToUp() }.position.toLatLng()
}
}
}
}