Felix Weber
Felix Weber

Reputation: 518

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

Upvotes: 35

Views: 10678

Answers (3)

Sebastian Esser
Sebastian Esser

Reputation: 1

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
}

Upvotes: 0

Harshal Wani
Harshal Wani

Reputation: 2349

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;
}

Upvotes: 5

Trần Minh To&#224;n
Trần Minh To&#224;n

Reputation: 796

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.

Upvotes: 77

Related Questions