0

je peux obtenir un annotationView d'un MKMapView simplement en lui passant une annotation:Fiable récupérer MKAnnotationView pour MKAnnotation

view(for annotation: MKAnnotation) -> MKAnnotationView? 

qui retourne un optional, parce que si une annotation est hors-champ, il pourrait ne pas avoir un annotationView associé.

Existe-t-il un moyen fiable de toujours récupérer un MKAnnotationView pour un MKAnnotation donné?

J'ai même essayé d'appeler manuellement le délégué de MKMapView, mais il se bloque mon application avec un message vague:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    if annotation is MKUserLocation { 
     return nil 
    } 

    return MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil) 
} 

utilisation:

Je développe un rotor VoiceOver personnalisé permettra aux utilisateurs de naviguer d'une broche à l'autre en effleurant l'écran. Donc, s'il y a 1 épingle sur chaque continent, ils peuvent se déplacer vers le haut ou vers le bas et la carte se centrera sur cette épingle sans avoir à parcourir la carte. La difficulté est que je dois dire au rotor sur mesure où aller, mais si une annotation est hors écran, il n'y a pas encore d'annotation associée, donc je ne peux pas instruire correctement le rotor.

Répondre

0

Dans ma situation, non seulement je besoin du MKAnnotationView, mais je devais aussi centrer la carte sur l'annotation, donc cela a fonctionné pour moi:

// imperative that this is called without animation. 
// setCenter will trigger the delegate method mapView(_:viewFor:) immediately 
self.mapView.setCenter(requestedAnnotation.coordinate, animated: false) 

// by the time we need the view, there's one associated with the annotation 
if let annotationView = self.mapView.view(for: requestedAnnotation) { 
    // we have an annotationView 
}