35
votes

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

3

3 Answers

77
votes

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.

5
votes

Following code works perfectly on all iOS 6 to iOS 9 devices:

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    // create a proper annotation view, be lazy and don't use the reuse identifier
    MKAnnotationView *view = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                reuseIdentifier:@"identifier"];

    // create a disclosure button for map kit
    UIButton *disclosure = [UIButton buttonWithType:UIButtonTypeContactAdd];

    [disclosure addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self
                                                                             action:@selector(disclosureTapped)]];
    view.rightCalloutAccessoryView = disclosure;

       view.enabled = YES;
       view.image = [UIImage imageNamed:@"map_pin"];


    return view;
}
0
votes

For Swift 4

func mapView(mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation is MKUserLocation {
        return nil
    }

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

    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)

    if pinView == nil {

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

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

    return pinView
}