I'm trying to get all member of the cluster(iOS 11) when tapping on cluster annotation on the map. Anyone knows how to get it?
This following code added cluster:
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if #available(iOS 11.0, *) {
            if annotation is MKClusterAnnotation {
                var anView = mapView.dequeueReusableAnnotationView(withIdentifier: "cluster")
                return anView
            }
        }
    }
When tap on the cluster is no way to get a member of the cluster
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if #available(iOS 11.0, *) {
        if view.annotation is MKClusterAnnotation {
            print(view.clusteringIdentifier)
            print(view.reuseIdentifier)
            //*** Need array list of annotation inside cluster here ***
         } else {
         }
     }
}
The only way to get cluster member is:
func mapView(_ mapView: MKMapView, clusterAnnotationForMemberAnnotations memberAnnotations: [MKAnnotation]) -> MKClusterAnnotation {
    print(memberAnnotations)
    return MKClusterAnnotation(memberAnnotations: memberAnnotations)
}
But no way to identify which one is the right tap cluster
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)
    }
  }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With