我想在 map 上添加一个带有手势图像的图钉。
实际上,在手势上添加 pin 功能效果很好,但没有图像,因为它必须是 MKAnnotationView
而不仅仅是 MKAnnotation
。
所以我收到了无法修复的奇怪警告:
最佳答案
override func viewDidLoad() {
super.viewDidLoad()
let annotation = MKPointAnnotation()
annotation.title = location.title
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
mapView.addAnnotation(annotation)
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
let identifier = "CustomAnnotation"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView!.canShowCallout = false
annotationView?.backgroundColor = UIColor.clear
annotationView!.image = UIImage(named: "map-pinpoint.png")!
} else {
annotationView!.annotation = annotation
}
return annotationView
}
关于swift - 如何在 map 上做手势时添加带有图像的图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51764238/