John
John

Reputation: 323

MapView clustering with custom annotations

I'm trying to cluster my mapView with custom annotations but cluster is never being called. This is my code:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation{
        return nil
    }
    
    let an = MKAnnotationView.init(annotation: annotation, reuseIdentifier: "AD")
    an.clusteringIdentifier = (annotation as! StopAnnotation).id!.joined(separator: "_")
    an.image = #imageLiteral(resourceName: "face")
    return an
}

This never triggers clustering, I also implemented this delegate:

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

Which is also never called. What am I doing wrong?

Upvotes: 1

Views: 2093

Answers (1)

Tuc3k
Tuc3k

Reputation: 1032

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.

Upvotes: 4

Related Questions