Search code examples
iosios5uiimageviewmkmapviewmkannotationview

How do I make a different left accessory image for each annotation appear?


I'm stuck here and I thought I'd ask if anyone can help me with this as I'm getting ready to tear my hair out.

I'm using the following tutorial/guide below and got as far the end and it works great for the one image but what I want is to be able to set a different image/thumbnail for each individual annotation

Tutorial/Guide

I've been trying to get this to work now for a while and I've just about had enough of looking at it. lol

I'm pretty new when it comes to xcode so that's why I'm struggling with it.


From OP's "suggested edit to the answer":

EDIT:

I Can't seem to get what you said to work and I'm stumped with it.

I've added a copy of my project at Dropbox below if it's possible for you to check it. It maybe abit of a mess at the moment as I've tried for ages and getting no where.

https://www.dropbox.com/sh/6ddhege240konzo/XrpuFYF5y5

I'm tempted just to figure out how to do the custom callout instead.

One thing I'm unsure about is delegates and how they really work.


Solution

  • You want to set the leftCalloutAccessoryView to a different image for each annotation.

    The leftCalloutAccessoryView is a property of MKAnnotationView so you need to set it in the viewForAnnotation delegate method (which is where you create and return an MKAnnotationView).

    The viewForAnnotation delegate method gets a reference to the annotation that you need to create a view for in the annotation parameter.

    So based on some property of annotation, you set leftCalloutAccessoryView accordingly.


    At the crudest level, you could set leftCalloutAccessoryView based on annotation.title.
    For example: if title is "SFO" set image to "apple", if title is "ATL" set image to "peach", etc.


    However, it's much better to create a separate property (in your annotation class that implements MKAnnotation) that clearly indicates what image to use for the annotation. This property could be the UIImage itself, the name of the image, a number, etc. -- whatever is best for your situation.

    When creating the annotation and before calling addAnnotation, you set this property of the annotation.

    Then in the viewForAnnotation delegate method, you set the leftCalloutAccessoryView based on the custom annotation property.

    For example, assuming an NSString property named imageName was added to the annotation class:

    MKAnnotationView *av = ... //or MKPinAnnotationView
    //typical dequeue and alloc/init code here
    
    if ([annotation isKindOfClass:[MyAnnotationClass class]])
    {
        //cast the annotation parameter to your custom class
        //so you can easily access the custom properties...
        MyAnnotationClass *myAnn = (MyAnnotationClass *)annotation;
    
        //create UIImage based on custom property of annotation...
        UIImage *img = [UIImage imageNamed:myAnn.imageName];
    
        //create UIImageView to use for the leftCalloutAccessoryView...
        UIImageView *iv = [[[UIImageView alloc] initWithImage:img] autorelease];
        //if using ARC, remove the autorelease above
    
        av.leftCalloutAccessoryView = iv;
    }
    
    return av;