如何检查注释是否聚类(MKMarkerAnnotationView 和 Cluster)

2024-01-06

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

我正在使用圆形碰撞对所有 MKAnnotationView 进行聚类,但当注释聚类时我必须实时检查。

我不知道该怎么做。

编辑(2018 年 4 月 1 日):

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

问题是,当选择注释并聚集时,当您放大注释时,注释仍处于选中状态,但处于“正常”状态。

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

下面的屏幕截图说明了我的问题:

1 - 选定注释 https://i.stack.imgur.com/UaNnB.jpg

2 - 注释集群 https://i.stack.imgur.com/J2qlS.jpg

3 - 簇后注释放大 https://i.stack.imgur.com/JyGnL.jpg

我认为这只是一个理解问题。

任何帮助将非常感激。 先感谢您


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

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

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

编辑(2018 年 4 月 1 日):

聚类标注有两个关键步骤:

首先,在注释聚集之前,MapKit 调用mapView:clusterAnnotationForMemberAnnotations:请求 MKClusterAnnotation 的函数memberAnnotations.

其次,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
         }
     }

     //...
 }

Or:

 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
             }
         }
     }

     //...
 }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何检查注释是否聚类(MKMarkerAnnotationView 和 Cluster) 的相关文章

随机推荐