Swift 如何检查批注是否群集(mkmarkerNotationView和群集)

Swift 如何检查批注是否群集(mkmarkerNotationView和群集),swift,mkmapview,ios11,mkannotation,mkannotationview,Swift,Mkmapview,Ios11,Mkannotation,Mkannotationview,我正在尝试使用ios11中添加到mapview的新功能 我正在使用圆碰撞对所有MKAnnotationView进行群集,但当批注群集时,我必须实时进行检查 我不知道怎么做 编辑(2018年4月1日): 更多信息:当我选择注释时,我会在调用didSelect方法时添加一个自定义CallOut视图,并在调用didSelect方法时删除CallOut 问题是当注释被选中并聚集时,当您放大注释时,注释仍然处于选中状态但处于“正常”状态 当选定注释的标注像DIDDENSELECT方法一样聚集时,我希望删除

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

我正在使用圆碰撞对所有MKAnnotationView进行群集,但当批注群集时,我必须实时进行检查

我不知道怎么做

编辑(2018年4月1日):

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

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

当选定注释的标注像DIDDENSELECT方法一样聚集时,我希望删除该标注

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

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

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

当新群集形成时,将调用
地图视图(uu-mapView:MKMapView,viewforannotation: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 {

    }
    //...
}

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

func映射视图(uMapView:MKMapView,ClusterAnnotationFormMemberAnnotations成员注释:[MKAnnotation])->MKClusterAnnotation

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

     //...
 }
在批注聚集之前,将调用此函数为
memberAnnotations
请求
MKClusterAnnotation
。因此,名为
memberAnnotations
的第二个参数指示要聚集的注释

编辑(2018年4月1日):

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

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

其次,MapKit将MKClusterAnnotation添加到MKMapView中,并将调用
mapView:viewForAnnotation:
函数来生成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 {

    }
    //...
}
var selectedAnnotation:MKAnnotation//所选注释

 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, didSelect view: MKAnnotationView) {

            // selected annotation belongs to a cluster
            if view.annotation is MKClusterAnnotation {
                print("selected a cluster")
            }
        }

谢谢你的解释,这对解决我的问题很有帮助。我仍然被卡住了,我只是添加了更多关于我的问题的信息,我将继续努力解决这个问题。谢谢你,我不知道。我只是被困在如何只获得选定的注释上。我想在群集时取消选择所选批注(仅此一个)@Seikikikisekash,我将更新您的问题的答案。非常感谢您的答案!我使用了:func mapView(mapView:MKMapView,ClusterAnnotationFormMemberAnnotations memberAnnotations:[MKAnnotation])->MKClusterAnnotation现在,我遇到了一个新问题,无法继续使用我的应用程序。当我放大/缩小注释时,我的应用程序崩溃。我尝试了一个解决方法:我的问题与之相同:当然,问题与取消选择无关,但我仍然被卡住。帮了我的忙!谢谢你的回复。