I have created a custom class for the MKAnnotation
@interface NeighborMapAnnotation : NSObject {
CLLocationCoordinate2D coordinate;
thats great! i tried to extend my custom annotation class by few properties. but i had no access inside of viewForAnnotation delegate. what i´ve missed and learned here is that i have to cast the additional property on my custom annotation class. now it works! thank you! :-)
The leftCalloutAccessoryView property is a property of the MKAnnotationView class (not in the MKAnnotation protocol).
In the viewForAnnotation delegate method of MKMapView, you provide an MKAnnotationView for your annotations. In that method, check if the annotation type is your custom annotation type using isKindOfClass and set the view's properties as needed.
In your NeighborMapAnnotation class, you could store the imageView as a UIImageView property and set that when creating the annotation.
In viewForAnnotation, you would set the view's leftCalloutAccessoryView to the annotation's imageView.
Edit:
Here's one way to implement it...
Add an image property to your custom annotation class:
@interface NeighborMapAnnotation : NSObject <MKAnnotation> {
...
UIImage *image;
}
...
@property (nonatomic, retain) UIImage *image;
@end
At the place where you create your annotations, set the image property:
NeighborMapAnnotation *neighbor = [[NeighborMapAnnotation alloc] initWithCoordinate:aCoordinate];
neighbor.image = [UIImage imageNamed:@"someimage.png"];
//the image could be different for each annotation
...
In the viewForAnnotation delegate method (make sure map view's delegate is set):
- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[NeighborMapAnnotation class]])
{
static NSString *AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if (!pinView)
{
pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
}
else
{
pinView.annotation = annotation;
}
UIImageView *leftCalloutView = [[UIImageView alloc]
initWithImage:((NeighborMapAnnotation *)annotation).image];
pinView.leftCalloutAccessoryView = leftCalloutView;
[leftCalloutView release];
return pinView;
}
return nil;
}