首页 > 解决方案 > MapKit 聚类在开始时没有正确聚类,它在平移后聚类

问题描述

我在 MapKit 中的集群有问题,它在开始时无法正常工作,我必须平移一些时间来强制 MapKit “重新计算”集群。下面的视频显示了这个问题。

https://youtu.be/5PK7uAV0F_8

我的代码说明

我有一个class MapKitViewController: UIViewController, MKMapViewDelegate方法类

override func loadView() {
    super.loadView()
    
    mapView = MKMapView(frame: view.bounds)
    mapView.delegate = self
    
    view.addSubview(mapView)
    mapView.translatesAutoresizingMaskIntoConstraints =  false
    mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    mapView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}

private func registerAnnotationViewClasses() {
    mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
    mapView.register(ClusterAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    registerAnnotationViewClasses()
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MyAnnotation {
        let v = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
        v.titleVisibility = .hidden
        v.clusteringIdentifier = "clusterID"
        return v
    } else {
        return nil
    }
}

这是我的集群注释视图类

class ClusterAnnotationView: MKAnnotationView {
    
    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        
        setUp()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setUp()
    }
    
    private func setUp() {
        collisionMode = .circle
    }

    override func prepareForDisplay() {
        super.prepareForDisplay()
        
        displayPriority = .defaultHigh

        if let clusterAnnotation = annotation as? MKClusterAnnotation {
            image = image(forMembersCount: clusterAnnotation.memberAnnotations.count)
        }
    }

    private func image(forMembersCount membersCount: Int) -> UIImage {
        let size = /* calculate size */
        let renderer = UIGraphicsImageRenderer(size: size)
        
        return renderer.image { _ in
            /* draw an image */
        }
    }
}

我能做些什么来解决这个问题?

标签: swiftmapkit

解决方案


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MyAnnotation {
        let v = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
        v.titleVisibility = .hidden
        v.clusteringIdentifier = "clusterID"
        return v
    } else {
        return nil
    }
}

您正在重新创建所有 annotationViews,可能每次平移时。

这些旨在重复使用,而不是每次都重新创建。所以 mapView 认为您正在用新对象替换您的 annotationViews(这就是您实际在做的事情)。这意味着现有视图不能平滑过渡 - 旧视图不再存在。

请参阅标题“配置注释视图”后的https://www.raywenderlich.com/7738344-mapkit-tutorial-getting-started#toc-anchor-009 ,了解如何重用注释视图。


推荐阅读