首页 > 解决方案 > 快速拖动地图或更改缩放级别时,MapKit 上的自定义集群注释崩溃

问题描述

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

-[MKPointAnnotation memberAnnotations]:无法识别的选择器发送到实例 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 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
    }
}

标签: iosswiftswiftuimapkitmkannotation

解决方案


崩溃的原因是您没有考虑地图套件请求的其他注释(例如MKUserLocation)。clusteringIdentifier由于设置为非nil值时的自动聚类,您正在触发此操作。

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. nil如果您显示用户位置,MKUserLocation如果您想要默认的蓝点,请不要忘记返回。


推荐阅读