处理地图和注释上的点击

问题描述 投票:0回答:0

我在 SwiftUI 中使用 UIViewRepresentable 地图,我需要将地图上的点击和注释上的点击作为两个不同的操作来处理。我使用 UIGestureRecognizerDelegate 来处理地图上的点击以获取坐标,并使用 mapView didSelect MKAnnotationView 来处理注释上的点击。问题是点击注释时 - UITapGestureRecognizer 也触发了。

final class MapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDelegate
{
    var parent: UIkitMapView
    var gRecognizer = UITapGestureRecognizer()
    
    init(_ parent: UIkitMapView) {
        self.parent = parent
        super.init()
        self.gRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapHandler))
        self.gRecognizer.delegate = self
        self.parent.mapView.addGestureRecognizer(gRecognizer)
    }
    
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        print(view.annotation?.title)
        parent.showPlaceDetails = true
      }
    
    @objc func tapHandler(_ gesture: UITapGestureRecognizer) {
        let location = gRecognizer.location(in: self.parent.mapView)
        let coordinate = self.parent.mapView.convert(location, toCoordinateFrom: self.parent.mapView)
        let pin = MKPointAnnotation()
        pin.coordinate = coordinate
        parent.setPinOnMap(point: pin)
        print("new pin")
    }
}
swiftui mapkit
© www.soinside.com 2019 - 2024. All rights reserved.