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

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

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

-[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
    }
}
ios swift swiftui mapkit mkannotation
1个回答
0
投票

导致崩溃的原因是,您没有考虑到地图工具包所要求的其他注释(例如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添加一个特殊情况。如果要显示用户位置,请不要忘记为默认的蓝点返回[C​​0]和nil

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