Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 11 Cluster member

Tags:

swift

mapkit

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

like image 843
MagicSpring Avatar asked Mar 10 '18 03:03

MagicSpring


1 Answers

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)

    }
  }
}
like image 53
benjuhn Avatar answered Nov 15 '22 09:11

benjuhn