我已经拖了两个 UIViewController,第一个显示带有注释和用户当前位置的 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
。现在一切都工作得很好!
关于ios - 如何使用 Swift 显示自定义注释,即使它位于用户当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59895584/