我正在尝试为地图图钉显示自定义注释图像。但是它始终显示默认的红色引脚。正确调用以下委托方法。
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {return nil}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.image = UIImage(named:"marker")
pinView!.sizeToFit()
}
else {
pinView!.annotation = annotation
}
return pinView
}
自定义引脚的初始化
var startPin:MapMarker = MapMarker(coordinate: self.mapView.userLocation.coordinate, title: "", subtitle: "")
self.mapView.addAnnotation(startPin)
自定义类
class MapMarker: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String
var subtitle: String
var image: UIImage?
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
}
}