我拖了UIViewControllers,第一个显示了带有注释和用户当前位置的MKMapKitView,在第二个viewController中,我将构造将显示在第一个viewController上的注释。
我发现,如果用户的位置和注释相同,则mapView将仅显示用户的当前位置。
这是我创建注释的代码:
func addAnnotiation(for locationCoordinate: CLLocationCoordinate2D) {
let annotationPoint = MKPointAnnotation()
annotationPoint.coordinate = locationCoordinate
mapView.addAnnotation(annotationPoint)
}
我的MKMapViewDelegate
方法:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "Annotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
return annotationView
}
我该如何解决此问题,所以如果注释和用户的位置相同,它将显示注释,而不仅是用户当前的位置?
问题非常简单。我以某种方式忘记了制作mapView.delegate = self
。现在一切都很好!