如何获取当前选中的注解名称?

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

简而言之,我当前的代码显示带有从另一个结构获取的自定义图像的注释,当用户单击注释时,它会显示另一个视图。

这是我的代码:


struct MapView: UIViewRepresentable {
    let annotations: [MKPointAnnotation]
    @Binding var showUserView: Bool

    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView(frame: .zero)
        mapView.delegate = context.coordinator
        mapView.showsUserLocation = true
        mapView.userTrackingMode = .followWithHeading
        mapView.mapType = .hybridFlyover
        let region = MKCoordinateRegion(
            center: CLLocationCoordinate2D(latitude: 0, longitude: 0),
            span: MKCoordinateSpan(latitudeDelta: 180, longitudeDelta: 360))
        mapView.setRegion(region, animated: false)
        mapView.showsBuildings = true
        mapView.addAnnotations(annotations)

        // Add gesture recognizer to handle annotation taps
        let gestureRecognizer = UITapGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.annotationTapped))
        mapView.addGestureRecognizer(gestureRecognizer)

        return mapView
    }

    func updateUIView(_ mapView: MKMapView, context: Context) {
        mapView.removeAnnotations(mapView.annotations)
        mapView.addAnnotations(annotations)
    }
func makeCoordinator() -> Coordinator {
    Coordinator(parent: self)
    }

    class Coordinator: NSObject, MKMapViewDelegate {
        let parent: MapView

        init( parent: MapView) {
            self.parent = parent
        }

        // Handle annotation taps by toggling showUserView
        @objc func annotationTapped() {
            parent.showUserView.toggle()
        }

        func mapView( mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
            guard let annotation = annotation as? MKPointAnnotation else {
                return nil
            }

            let identifier = "CustomAnnotation"
            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

            if annotationView == nil {
                annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
                annotationView?.canShowCallout = true
                annotationView?.image = UIImage(named: "first.png")
            } else {
                annotationView?.annotation = annotation
            }

            return annotationView
        }
    }
}

我尝试使用以下代码更改 Coordinator 结构,但是点击手势功能中没有任何效果:

class Coordinator: NSObject, MKMapViewDelegate {
    let parent: MapView
    var selectedAnnotationName: String?

    // ... existing code ...

    // Handle annotation taps by toggling showUserView and setting the selectedAnnotationName
    @objc func annotationTapped(_ sender: UITapGestureRecognizer) {
        if let annotationView = sender.view as? MKAnnotationView, let annotation = annotationView.annotation as? MKPointAnnotation {
            parent.showUserView.toggle()
            selectedAnnotationName = annotation.title ?? ""
        }
    }

    // ... existing code ...
}
swift swiftui mapkit mkannotation
1个回答
0
投票

目前,点击手势识别器配置为在用户点击地图视图上的任何地方时触发,而不仅仅是特定的注释。然而,被调用的方法期望手势识别器安装在特定的注释视图上,但事实并非如此,所以什么也没有发生

而不是设置你自己的点击手势识别器,你的

Coordinator
应该实现
mapView(_:didSelect:)
,当用户选择注释时 MapKit 调用它,它会给你被点击的视图。你可以从那里做任何你想做的操作。如果您只需要注释本身(而不是注释视图)并且您的构建目标至少是 iOS 16.0,那么实现它可能会更直接直接采用
MKAnnotation
的版本

© www.soinside.com 2019 - 2024. All rights reserved.