I'm trying to display a custom annotation image for a map pin. However it always shows the default red pin. The following delegate method gets called correctly.
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
}
Initialisation of custom pin
var startPin:MapMarker = MapMarker(coordinate: self.mapView.userLocation.coordinate, title: "", subtitle: "")
self.mapView.addAnnotation(startPin)
Custom class
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
}
}