授权检查后,要获取用户位置,我正在调用此CLLocation委托函数:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let latestLocation = locations.first else { return }
if currentCoordinate == nil {
zoomToLocation(with: latestLocation.coordinate)
}
currentCoordinate = latestLocation.coordinate
}
然后我放大到用户的位置:
func zoomToLocation(with coordinate: CLLocationCoordinate2D) {
let regionDimension : Double = 1000
let zoomRegion = MKCoordinateRegion.init(center: coordinate, latitudinalMeters: regionDimension, longitudinalMeters: regionDimension)
mapView.setRegion(zoomRegion, animated: true)
}
到目前为止很好。但是我正在加载一些具有自定义批注的“目的地”,因此我需要调用此委托函数:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
}
annotationView?.image = UIImage(named: "icon_marker")
annotationView?.canShowCallout = true
return annotationView
}
并且它将用相同的自定义标记替换用户位置的蓝点。这是一个非常奇怪的用户体验。如何从自定义注释中排除“我的位置”?
问题是您没有检查传入的annotation
。检查一下!如果它是MKUserLocation,则返回nil
。