XCode9.4.1 Swift:在地图注释上显示图像而不是文本



您知道在MKMapView中单击注释时标题和副标题是如何显示的。如何显示图像而不是字幕文本?我正在使用的atm:

class customPin: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
init(pinTitle: String, pinSubTitle: String,    
Location:CLLocationCoordinate2D) {
self.title = pinTitle
self.subtitle = pinSubTitle
self.coordinate = Location
}
}

我试着用"UIImage"(在"字幕"处(代替"字符串",但也不起作用

感谢您的帮助,Gus

MKAnnotation只管理引脚的数据,而不管理外观。MKAnnotationView是引脚的视觉表示。您可以使用mapView(_:viewFor:)方法操作和生成:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "customPin"
var annotationView: MKAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView {
annotationView = dequeuedView
annotationView.annotation = annotation
} else {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView.canShowCallout = true
annotationView.image = UIImage(named: "example")
}   
return annotationView
}

另请参阅:MKAnnotationView 上的苹果开发者文档

最新更新