Rutger Huijsmans Rutger Huijsmans - 3 months ago 14
Swift Question

I want to loop over all the annotations from my Cluster Annotation

I'm using the cocoapod FBAnnotationClusteringSwift and it's possible to group my annotations together. However I want to loop over all those annotations that are clustered together when the cluster annotation is tapped.

How do I do this?

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
if (view.annotation!.isKindOfClass(FBAnnotationCluster) == true){

//I WANT TO LOOP OVER ALL ANNOTATIONS IN THE CLUSTER HERE

}
if (view.annotation!.isKindOfClass(ItemAnnotation) == true){
let annotation = view.annotation! as? ItemAnnotation
if let annotation = annotation, let item = annotation.item, d = delegate{
d.itemAnnotationPressed(item)
}
}
}

Answer
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
        if (view.annotation!.isKindOfClass(FBAnnotationCluster) == true){
            let annotation = view.annotation! as? FBAnnotationCluster

            var itemListFromAnnotation = [Item]()

            for annotation in (annotation?.annotations)! {
                let itemAnnotation = annotation as? ItemAnnotation
                itemListFromAnnotation.append((itemAnnotation?.item)!)
            }

            if let d = delegate{
                d.itemClusterAnnotationPressed(itemListFromAnnotation)
            }
        }
        if (view.annotation!.isKindOfClass(ItemAnnotation) == true){

            mapView.deselectAnnotation(view.annotation, animated: false)

            let annotation = view.annotation! as? ItemAnnotation
            if let annotation = annotation, let item = annotation.item, d = delegate{
                d.itemAnnotationPressed(item)
            }
        }
    }
Comments