SwiftUI点击MapAnnotation

时间:2020-11-06 18:11:59

标签: ios swift swiftui mapkit

我想在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
        )
    )

这是标记在模拟器中的外观(我也尝试过将它们放大,因此无法通过点击不击中它们)

This is how the markers look in the simulator

希望有人可以提供帮助!

0 个答案:

没有答案