0

我可以通過簡單地傳遞一個annotation得到一個MKMapViewannotationView可靠檢索MKAnnotationView爲MKAnnotation

view(for annotation: MKAnnotation) -> MKAnnotationView? 

返回一個optional,因爲如果註釋是屏幕外,它可能不具有關聯annotationView。

是否有一種可靠的方式總是檢索給定的MKAnnotationViewMKAnnotation

我甚至手動打過電話的MKMapView的委託,但它崩潰我的應用程序一個模糊的信息:

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

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

使用案例:

我正在開發一個定製的VoiceOver轉子,該轉子將允許用戶通過輕彈屏幕從一個針腳導航到另一個針腳。所以如果每個大陸上有1個針,他們可以向上或向下滑動,地圖將在該針上居中,而不必平移地圖。困難是我需要告訴定製轉子去哪裏,但是如果註釋是離屏的方式,那麼沒有與它關聯的註釋視圖,所以我不能正確地指示轉子。

回答

0

在我的情況,我不僅需要MKAnnotationView,但我還需要到中心在地圖上標註,所以這個工作對我來說:

// 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 
}