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;