在 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
}
}
}
//...
}