2

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
}

}

gpichler
  • 2,181
  • 2
  • 27
  • 52
  • 4
    In viewForAnnotation, change MKPinAnnotationView to plain MKAnnotationView and remove setting of animatesDrop. –  Apr 20 '15 at 21:47
  • ok, i've changed these two lines, but it still doesn't show the image. does the image need to have a specific size? – gpichler Apr 21 '15 at 06:59
  • Specific size is not required but something reasonable so user can see it (around 32x32). Does it still show a red pin or is it now invisible? Make sure marker.png is actually added to project and named exactly like that. How do you know delegate method is getting called for sure? –  Apr 21 '15 at 11:51

0 Answers0