如何检查注释是否群集(MKMarkerAnnotationView和Cluster) [英] How to check if annotation is clustered (MKMarkerAnnotationView and Cluster)

查看:245
本文介绍了如何检查注释是否群集(MKMarkerAnnotationView和Cluster)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ios11中添加到mapview的新功能.

I'm trying to work with the new features added to the mapview in ios11.

我正在将我所有的MKAnnotationView与圆碰撞聚在一起,但是当聚类成簇时,我必须实时检查.

I'm clustering all my MKAnnotationView with circle collision but I have to check in real time when the annotation becomes clustered.

我不知道该怎么做.

编辑(4/1/2018):

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

More informations : When I select an annotation I add a custom CallOutView when the didSelect method is called and remove the CallOut when the didDeselect method is called.

问题是当注释被选中并变为群集时,当您放大注释仍处于正常"状态时,将其放大.

The issue is when an annotation is selected and becomes clustered, when you zoom in the annotation is still selected but in "normal" state.

我希望在选中的注释像didDeselect方法一样成簇时将其删除.

I want to remove the CallOut of my selected annotation when it become clustered like the didDeselect method.

下面的屏幕快照说明了我的问题:

Below screenshot to illustrate my issue :

1-已选择注释

2-注释群集

3-群集后的注释放大

我认为这只是理解问题.

I think it's just an issue of comprehension.

任何帮助将不胜感激. 预先谢谢你

Any help would be really appreciated. Thank you in advance

推荐答案

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

In iOS 11, Apple also introduce a new callback in MKMapViewDelegate:

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

在注释聚类之前,将调用此函数为memberAnnotations请求一个MKClusterAnnotation.因此,名为memberAnnotations的第二个参数表示要聚类的注释.

Before annotations become clustered, this function will be called to request a MKClusterAnnotation for memberAnnotations. So the second parameter named memberAnnotations indicates the annotations to be clustered.

编辑(4/1/2018):

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

There are two key steps for cluster annotation:

首先,在注释成为簇之前,MapKit调用mapView:clusterAnnotationForMemberAnnotations:函数为memberAnnotations请求MKClusterAnnotation.

First, before annotations become clustered, MapKit invoke mapView:clusterAnnotationForMemberAnnotations: function to request a MKClusterAnnotation for memberAnnotations.

第二,MapKit将MKClusterAnnotation添加到MKMapView,然后调用mapView:viewForAnnotation:函数以生成MKAnnotationView.

Secondly, MapKit add the MKClusterAnnotation to MKMapView, and mapView:viewForAnnotation: function will be called to produce a MKAnnotationView.

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

So you can deselect annotation in either of the two steps, like this:

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

     //...
 }

这篇关于如何检查注释是否群集(MKMarkerAnnotationView和Cluster)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆