快速拖动地图或更改缩放级别时,MapKit 上的自定义集群注释崩溃

Custom clustered annotations on MapKit crash when rapidly dragging the map or changing the zoom-level

实施集群自定义注释的方法后,只要通过滚动或更改缩放级别快速调整地图视图,应用程序就会崩溃。

-[MKPointAnnotation memberAnnotations]: unrecognized selector sent to instance 0x281396c00

我的猜测是编译器试图检索注释信息,但找不到数据。由于我是 Swift 的新手,所以我看不出我遗漏了什么。非常感谢您的帮助。

我有一个非常基本的设置可以在 SwiftUI 中显示地图。在主文件中,我从 MapView.swift

调用 MapView
struct MapView: UIViewRepresentable {

    @ObservedObject var store = DataStoreMap()

    func makeCoordinator() -> MapViewCoordinator {
        MapViewCoordinator(self)
    }

    func makeUIView(context: Context) -> MKMapView{
        MKMapView(frame: .zero)
    }

    func updateUIView(_ view: MKMapView, context: Context){

        let location = getUserLocation()
        let chargers = store.chargers

        let coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
        let span = MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03)
        let region = MKCoordinateRegion(center: coordinate, span: span)
        view.setRegion(region, animated: true)

        for charger in chargers {
            let annotation = MKPointAnnotation()
            annotation.coordinate = CLLocationCoordinate2D(latitude: charger.addressInfo.latitude, longitude: charger.addressInfo.longitude)
            view.delegate = context.coordinator
            view.addAnnotation(annotation)
        }

    }
}

同一文件中还包含我的自定义注释 class。

class MapViewCoordinator: NSObject, MKMapViewDelegate {

    var mapViewController: MapView

    init(_ control: MapView) {
        self.mapViewController = control
    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        //Custom View for Annotation
        var annotationView = MKMarkerAnnotationView()
        annotationView.canShowCallout = true

        let identifier = "laadpaal"

        if let dequedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView {
            annotationView = dequedView
        } else {
            annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        }

        annotationView.markerTintColor = .some(.systemBlue)
        annotationView.glyphImage = UIImage(named: "car1")
        annotationView.glyphTintColor = .yellow
        annotationView.clusteringIdentifier = identifier

        return annotationView
    }
}

导致崩溃的原因是您没有考虑地图工具包请求的其他注释(例如 MKUserLocation)。当您将 clusteringIdentifier 设置为非 nil 值时,由于自动集群而触发了此问题。

如果您想自己处理注释,只需 return nil,因此 MKMapView 使用默认处理:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation { return nil }
        if annotation is MKClusterAnnotation { return nil }

        let view = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier", for: annotation)
        view.clusteringIdentifier = "clusterIdentifer"
        // …

        return view
    }

如果您想自定义集群注释,只需为 MKClusterAnnotation 添加一个特例即可。如果您显示用户位置,请不要忘记 return nil for MKUserLocation 如果您想要默认的蓝点。