iOS MapKit开发时, 往地图上添加自定义MKAnnotation图钉时, 经常会遇到图钉偏移的问题。
// カスタムMKAnnotation作成 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { guard annotation is MKPointAnnotation else { return nil } let identifier = "Annotation" + String(arc4random()) var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) if annotationView == nil { annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier) annotationView!.canShowCallout = true let btn = UIButton(type: .detailDisclosure) annotationView!.rightCalloutAccessoryView = btn } else { annotationView!.annotation = annotation let image = UIImage(named: annotationName)!.scaleImage(scaleSize: 0.8) // 重要通过设定annotationView的offset可以解决偏移的问题 let offset = CGPoint(x: 0, y: -(image.size.height / 2)) annotationView!.image = UIImage(named: annotationName)!.scaleImage(scaleSize: 0.8) annotationView!.centerOffset = offset annotationView!.displayPriority = .required formatAnnotation(pinView: annotationView!, forMapView: mapView) return annotationView }