Swift

MapKit 往地图上添加MKAnnotation 偏移的问题

iOS MapKit开发时,  往地图上添加自定义MKAnnotation图钉时, 经常会遇到图钉偏移的问题。
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// カスタム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
}
// カスタム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 }
       // カスタム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
        }