首页 > 解决方案 > 如何从 MKClusterAnnotation 获取具有相同坐标的注释?

问题描述

我目前正在开发一个应用程序,我在其中使用来自苹果的 mapkit 的 MKClusterAnnotations 加载不同类型的注释。我们知道 MKClusterAnotation 是An annotation that groups two or more distinct annotations into a single entity. 这是否可以从具有相同坐标的集群中获取或删除注释?

例如,如果有 2 个具有相同坐标的集群,比如lat: 45.757036789379, long: 4.8989974471952另一个集群lat: 45.757049657763, long: 4.8989858812774,那么当我放大或选择集群时,它应该从集群中删除,否则它应该从集群成员注释中获取。

在 Map DidSelect 委托下方,

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    if let annotation = view.annotation, annotation.isKind(of: MKClusterAnnotation.self) {

        let cluster: MKClusterAnnotation = annotation as! MKClusterAnnotation

        if cluster.memberAnnotations.count > 1 {

            mapView.showAnnotations(cluster.memberAnnotations, animated: true)

            self.isMapDidSelect = true
        } else { //In this case need to fetch memberAnnotations when cluster fails to remove the grouped annotations itself
} } }

而且我还在视图上为注释委托设置了集群标识符,

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation.isKind(of: MKUserLocation.self) {

        let annotationView: CurrentUserAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: CurrentUserAnnotationView.identifier(), for: annotation) as! CurrentUserAnnotationView

        annotationView.selectedMapVisibility = viewModel.selectedMapVisibility.value

        annotationView.photoUrl = viewModel.user.value?.photoUrl

        return annotationView
    }

    if annotation.isKind(of: MKClusterAnnotation.self) {

        if let annotationView: MKAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier) as? ClusterAnnotationView {

            return annotationView
        }
    }

    let annotationView = mapView.annotationViewFor(annotation: annotation)
    annotationView?.clusteringIdentifier = "clusterId"
    return annotationView
}

标签: iosswiftmapkitmkmapviewdelegatemkclusterannotation

解决方案


推荐阅读