在我的 UIViewRepresentable updateUIView (SwiftUI) 中设置 MKPointAnnotation() pin 的标题后,如何 show/hide?

How can I show/hide the title of my MKPointAnnotation() pin after I set it inside of my UIViewRepresentable updateUIView (SwiftUI)?

我想在点击图钉后切换 MKPointAnnotation 标题的可见性。我尝试直接在
中更改标题 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) 但它告诉我它只是一个 get 属性 并且我无法在我的 Coordinator class.

中更改它

如有任何帮助,我们将不胜感激!

这里是相关代码...

import SwiftUI
import MapKit
import CoreLocationUI

struct MapViewTest: UIViewRepresentable {
    
    @EnvironmentObject var viewModel: MapViewModel
    
    @Binding var region: MKCoordinateRegion
    @Binding var lineCoordinates: [[CLLocationCoordinate2D]]
    
    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView()
        mapView.delegate = context.coordinator
        mapView.region = region
        mapView.showsUserLocation = true
        
        return mapView
    }
    
    func updateUIView(_ view: MKMapView, context: Context) {
        view.setRegion(region, animated: true)
        
        for i in viewModel.locations {
            let pin = MKPointAnnotation()
            pin.coordinate = i.coordinate
            pin.title = i.name
            view.addAnnotation(pin)
        }
        
        for i in lineCoordinates{
            let polyline = MKPolyline(coordinates: i, count: i.count)
            view.addOverlay(polyline)
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    
}

class Coordinator: NSObject, MKMapViewDelegate {
        
    var parent: MapViewTest
    
    init(_ parent: MapViewTest) {
        self.parent = parent
    }
    
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        
    }
    
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if let routePolyline = overlay as? MKPolyline {
            let renderer = MKPolylineRenderer(polyline: routePolyline)
            renderer.strokeColor = UIColor.systemBlue
            renderer.lineWidth = 10
            return renderer
        }
        return MKOverlayRenderer()
    }
}

无论何时制作 MapKit 注释,都应包含 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?。此功能允许您配置您的引脚,但它也允许您重新使用未使用的引脚。每当图钉从地图上消失(四处滚动等)时,该图钉不会被销毁,但它会被保留以在需要另一个图钉时重复使用。这样可以节省处理器和内存。

在您的 Coordinator class 中添加以下函数:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    // create a unique identifier for pin reuse
    let identifier = "Placemark"
    
    // see if there already is a created pin
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
    
    if annotationView == nil {
        // there wasn't a pin, so we make a new one
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        
        // this is where your title is allowed to be shown when tapping the pin
        annotationView?.canShowCallout = true
        
        // this gives you an information button in the callout if needed
        // if you use the rightCalloutAccessoryView you must implement:
        // func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    } else {
        // we have an old annotation, so update it
        annotationView?.annotation = annotation
    }
    
    return annotationView
}