When you add your annotation, you can set the image in (code mixed using apple example):
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[Annotation class]])
{
return [self registerAnnotation:annotation withIdentifier:@"AnnotationID" withImage:self.personImage.image];
}
else
{
return nil;
}
}
- (MKAnnotationView*)registerAnnotation:(id<MKAnnotation>)annotation withIdentifier:(NSString*)identifier withImage:(UIImage*)image
{
NSLog(@"annotationIdentifier: %@", identifier);
MKAnnotationView *flagAnnotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (flagAnnotationView == nil)
{
flagAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
flagAnnotationView = [self resizeAnnotationFromAnnotation:flagAnnotationView andSetImage:image];
}
else
{
flagAnnotationView.annotation = annotation;
}
return flagAnnotationView;
}
- (MKAnnotationView*)resizeAnnotationFromAnnotation:(MKAnnotationView*)annotationView andSetImage:(UIImage*)flagImage
{
annotationView.canShowCallout = YES;
annotationView.opaque = NO;
CGRect resizeRect = [self resizeAnnotationFromSize:flagImage.size];
UIImage *resizedImage = [self resizeImageFromCGRect:resizeRect andImage:flagImage];
annotationView.image = resizedImage;
// offset the flag annotation so that the flag pole rests on the map coordinate
annotationView.centerOffset = CGPointMake(annotationView.centerOffset.x + annotationView.image.size.width/2, annotationView.centerOffset.y - annotationView.image.size.height/2);
return annotationView;
}
- (UIImage*)resizeImageFromCGRect:(CGRect)resizeRect andImage:(UIImage*)flagImage
{
UIGraphicsBeginImageContext(resizeRect.size);
[flagImage drawInRect:resizeRect];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}
- (CGRect)resizeAnnotationFromSize:(CGSize)size
{
// size the flag down to the appropriate size
CGRect resizeRect;
resizeRect.size = size;
CGSize maxSize = CGRectInset(self.view.bounds, [self annotationPadding], [self annotationPadding]).size;
maxSize.height -= self.navigationController.navigationBar.frame.size.height + [self calloutHeight];
if (resizeRect.size.width > maxSize.width)
resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);
if (resizeRect.size.height > maxSize.height)
resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);
resizeRect.origin = CGPointMake(0.0, 0.0);
return resizeRect;
}