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

标签 ios swift swiftui mapkit mkannotation

在实现一种集群自定义注释的方法后,只要通过滚动或更改缩放级别快速调整 map View ,应用程序就会崩溃。

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

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

我有一个非常基本的设置来在 SwiftUI 中显示 map 。在主文件中,我从 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
    }
}

最佳答案

导致崩溃的原因是您没有考虑 map 工具包请求的其他注释(例如 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 添加一个特例即可。如果您显示用户位置,如果您想要默认的蓝点,请不要忘记为 MKUserLocation 返回 nil

关于ios - 快速拖动 map 或更改缩放级别时,MapKit 上的自定义集群注释崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61174126/

相关文章:

ios - UIButton 不调用 ImageView 中的处理函数

swift - 如何通过 reactive(RxSwift) 调用替换 UICollectionViewDelegateFlowLayout?

swift - fatal error : unexpectedly found nil while unwrapping an Optional value - using UITableView

ios - 如何在 Swift 中设置返回按钮文本

ios - SwiftUI 如何在 UIBarButtonItem 上添加操作?

ios - 在 SwiftUI 中使列表行可选择

ios - UIWebView 显示 URL 而不是网页

ios - Swift iOS EventKit 中的每周重复事件

ios - swift 中不同类型的闭包语法——哪一个是正确的?

ios - 是否可以在 SwiftUI 中对某个路径上的 View 进行动画处理