我想在SwiftUI中创建一个MapView来获取标记列表并显示它们。但我也希望用户能够单击/点击它们。如果他们被窃听,则应该翻转布尔值将其标记为完成。
我为标记创建了一个模型数组:
struct Marker: Identifiable {
let id = UUID()
var coordinate : CLLocationCoordinate2D
var done : Bool
}
let markers = [
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.882975, longitude: 10.558585), done: true),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.883329, longitude: 10.559225), done: true),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.883296, longitude: 10.558216), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.882614, longitude: 10.559574), done: true),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.882425, longitude: 10.559263), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.882607, longitude: 10.557679), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.883246, longitude: 10.557652), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.883753, longitude: 10.557711), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.884018, longitude: 10.558710), done: false),
Marker(coordinate: CLLocationCoordinate2D(latitude: 51.881783, longitude: 10.559713), done: false),
]
问题是当我点击标记时什么也没有发生。我尝试在.onTapGesture { }
这是我的地图的代码:
Map(coordinateRegion: $region, annotationItems: markers) { marker in
MapAnnotation(coordinate: marker.coordinate) {
Circle()
.onTapGesture {
// do something
}
}
}
$region
是这个:
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 51.882879, longitude: 10.558918),
span: MKCoordinateSpan(
latitudeDelta: 0.01,
longitudeDelta: 0.01
)
)
这是标记在模拟器中的外观(我也尝试过将它们放大,因此无法通过点击不击中它们)
希望有人可以提供帮助!