Search code examples
mkmapviewmapkitmkannotationios9mkannotationview

MapKit not showing custom Annotation pin image on iOS9


My code worked fine from iOS 7 to 8. With the update yesterday the custom images on my pins were replaced by the standard pin image. Any suggestions?

My code:

extension ViewController: MKMapViewDelegate {

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView! {

        if annotation is MKUserLocation {
            return nil
        }

        let reuseId = String(stringInterpolationSegment: annotation.coordinate.longitude)

        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView

        if pinView == nil {

            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.image = getRightImage(annotation.title!!) 
        }

        let button = UIButton(type: UIButtonType.DetailDisclosure) 
        pinView?.rightCalloutAccessoryView = button

        return pinView
    }
}

The function to get the image returns a UIImage based on the name:

func getRightImage (shopName:String)-> UIImage{

    var correctImage = UIImage()

    switch shopName
    {
    case "Kaisers":
        correctImage = UIImage(named: "Kaisers.jpg")!
    default:
        correctImage = UIImage(named: "sopiconsmall.png")!

    }

    return correctImage
}

No the map looks like this: ios9 without images


Solution

  • Instead of creating an MKPinAnnotationView, create a plain MKAnnotationView.

    The MKPinAnnotationView subclass tends to ignore the image property since it's designed to show the standard red, green, purple pins only (via the pinColor property).

    When you switch to MKAnnotationView, you'll have to comment out the animatesDrop line as well since that property is specific to MKPinAnnotationView.