2017-12-20 6 views
0

Ich habe eine einfache Kartenansicht:Warum clusterAnnotationForMemberAnnotations in MKMapView nicht aufgerufen wird?

@IBOutlet private var mapView: MKMapView! 

Dann eins nach dem anderen I Anmerkungen hinzufügen:

mapView.addAnnotation(Annotation(user: user)) 

und zeigen sie alle:

mapView.showAnnotations(mapView.annotations, animated: true) 

I implementiert auch das Verfahren:

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

aber das wird überhaupt nicht aufgerufen. Warum?

enter image description here enter image description here

+0

Das gleiche heute im iOS 11.2-Simulator bemerkt. Deklariert, dass die umschließende Klasse der Delegat ist, aber es scheint einfach nicht zu funktionieren. – edwardmp

Antwort

3

Es scheint, als ob es erforderlich ist, die clusteringIdentifier f eingestellt oder die MKAnnotationView. Das hat für mich gearbeitet:

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

@ Peter Antwort für mich gearbeitet, aber ich finde es mehr intuitiv die clusterIdentifier direkt auf Ihrem MKAnnotationView zu setzen, anstatt eine delegierte Methode der Verwendung zu setzen in. Zum Beispiel:

class VehicleAnnotationView: MKAnnotationView { 
    override var annotation: MKAnnotation? { 
     willSet { 
      clusteringIdentifier = "1" 

      canShowCallout = true 
      calloutOffset = CGPoint(x: -5, y: 5) 
      rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 

      image = UIImage(named: "MapVehiclePin") 

     } 
    } 
} 
Verwandte Themen