Why Clusterannotationformemberannotations in Mkmapview Is Not Called

Why clusterAnnotationForMemberAnnotations in MKMapView is not called?

It appears as though it is required to set the clusteringIdentifier for the MKAnnotationView. This has worked for me:

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

MapView clustering with custom annotations

Make sure that clusteringIdentifier isn't unique for every cluster. Since you're passing (annotation as! StopAnnotation).id! I'm guessing it is uniq.

From Apple documentation :

Clustering occurs when there is a collision between multiple annotation views with the same identifier on the map surface.

You can read more here.

How to check if annotation is clustered (MKMarkerAnnotationView and Cluster)

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

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

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.

EDIT (4/1/2018):

There are two key steps for cluster annotation:

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

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

//...
}

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

//...
}

iOS 11 Cluster member

Clusters have a "memberAnnotations" property:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if #available(iOS 11.0, *) {
if let cluster = view.annotation as? MKClusterAnnotation {
//*** Need array list of annotation inside cluster here ***
let arrayList = cluster.memberAnnotations

// If you want the map to display the cluster members
mapView.showAnnotations(cluster.memberAnnotations, animated: true)

}
}
}

Annotations are hidden when zooming out

The current user's annotation doesn't have a cluster identifier. If you provide a cluster identifier for the current user's annotation, it works.



Related Topics



Leave a reply



Submit