【问题标题】:How to check if annotation is clustered (MKMarkerAnnotationView and Cluster)如何检查注解是否集群(MKMarkerAnnotationView 和集群)
【发布时间】:2017-12-31 08:03:12
【问题描述】:

我正在尝试使用 ios11 中添加到地图视图的新功能。

我将我所有的 MKAnnotationView 与圆碰撞聚集在一起,但我必须实时检查注释何时聚集。

我不知道该怎么做。

编辑(2018 年 4 月 1 日):

更多信息:当我选择注释时,我会在调用 didSelect 方法时添加自定义 CallOutView,并在调用 didDeselect 方法时删除 CallOut。

问题是当一个注释被选中并成为集群时,当你放大注释时仍然被选中但处于“正常”状态。

当我选择的注释像 didDeselect 方法一样聚集时,我想删除它的 CallOut。

下面的截图来说明我的问题:

1 - Annotation Selected

2 - Annotation Clustered

3 - Annotation Zoom In after cluster

我认为这只是理解的问题。

任何帮助将不胜感激。 提前谢谢你

【问题讨论】:

    标签: swift mkmapview ios11 mkannotation mkannotationview


    【解决方案1】:

    在 iOS 11 中,Apple 还在 MKMapViewDelegate 中引入了新的回调:

    func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation

    在注解聚集之前,将调用此函数来为memberAnnotations 请求MKClusterAnnotation。所以第二个参数memberAnnotations表示要聚类的注解。

    编辑(2018 年 4 月 1 日):

    集群标注有两个关键步骤:

    首先,在注解聚集之前,MapKit 调用mapView:clusterAnnotationForMemberAnnotations: 函数为memberAnnotations 请求一个MKClusterAnnotation。

    其次,MapKit 将 MKClusterAnnotation 添加到 MKMapView 中,会调用mapView:viewForAnnotation: 函数生成一个 MKAnnotationView。

    因此您可以在两个步骤中的任何一个中取消选择注释,如下所示:

    var selectedAnnotation: MKAnnotation? //the selected annotation

     func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation {
         for annotation in memberAnnotations {
             if annotation === selectedAnnotation {
                 mapView.deselectAnnotation(selectedAnnotation, animated: false)//Or remove the callout
             }
         }
    
         //...
     }
    

    或者:

     func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
         if let clusterAnnotation = annotation as? MKClusterAnnotation {
             for annotation in clusterAnnotation.memberAnnotations {
                 if annotation === selectedAnnotation {
                     mapView.deselectAnnotation(selectedAnnotation, animated: false)//Or remove the callout
                 }
             }
         }
    
         //...
     }
    

    【讨论】:

    • 谢谢,我不知道。我只是坚持如何只获取选定的注释。我想在集群时取消选中选中的注解(只有这个)
    • @SeikiKisekash ,我为您的问题更新了我的答案。
    • 非常感谢您的回答!我使用过: func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation 现在,我遇到了一个新问题,无法继续在我的应用程序上工作。当我放大/缩小注释时,我的应用程序崩溃。我尝试解决这个问题:forums.developer.apple.com/thread/89427 我的问题与以下内容相同:openradar.appspot.com/36131654 当然,问题与取消选择无关,但我仍然卡住了。
    【解决方案2】:

    当一个新的集群形成时,mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 将被调用以请求该集群的新视图。

    你可以这样检查:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        //...
        if annotation is MKClusterAnnotation {
            //This is your new cluster.
        }
        //Alternatively if you need to use the values within that annotation you can do
        if let cluster = annotation as? MKClusterAnnotation {
    
        }
        //...
    }
    

    【讨论】:

    • 感谢您的解释,对解决我的问题很有帮助。我仍然卡住了,我只是添加了有关我的问题的更多信息,我将继续努力解决这个问题。
    【解决方案3】:

    我在这里聚会迟到了两年,但只想说您还可以通过以下方式检测是否选择了集群与单个注释:

            func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    
                // selected annotation belongs to a cluster
                if view.annotation is MKClusterAnnotation {
                    print("selected a cluster")
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-01
      • 2018-07-02
      • 2012-01-04
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      相关资源
      最近更新 更多