为什么不调用MKMapView中的clusterAnnotationForMemberAnnotations?

Bar*_*zyk 0 mapkit ios swift

我有一个简单的地图视图:

@IBOutlet private var mapView: MKMapView!
Run Code Online (Sandbox Code Playgroud)

然后我一个接一个地添加注释:

mapView.addAnnotation(Annotation(user: user))
Run Code Online (Sandbox Code Playgroud)

并向他们展示:

mapView.showAnnotations(mapView.annotations, animated: true)
Run Code Online (Sandbox Code Playgroud)

我也实现了这个方法:

func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation {
    print(memberAnnotations)
    return MKClusterAnnotation(memberAnnotations: memberAnnotations)
}
Run Code Online (Sandbox Code Playgroud)

但这根本不被召唤.为什么?

在此输入图像描述 在此输入图像描述

小智 8

看起来好像需要clusteringIdentifier为MKAnnotationView 设置它.这对我有用:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier, for: annotation)
    annotationView.clusteringIdentifier = "identifier"
    return annotationView
}
Run Code Online (Sandbox Code Playgroud)