0

I have a MKPinAnnotationView and in my viewForAnnotation method, I do the following:

customPinView.image = [UIImage imageNamed:@"blah.png"];

I have added blah.png to my resources (by dragging the file in)

But I still see the stock pin and not my image. Am I doing something wrong? Here is the complete code:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
    static NSString *AnnotationViewID = @"annotationViewID";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

    if (annotationView == nil)
    {
        annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
    }

    annotationView.image = [UIImage imageNamed:@"blah.png"];
    annotationView.annotation = annotation;

    return annotationView;
}
Luke
  • 11,426
  • 43
  • 60
  • 69
DeShawnT
  • 479
  • 2
  • 7
  • 12
  • 1
    In the question you say you have a "MKPinAnnotationView" but in the code you are (correctly) creating a "MKAnnotationView" so which is it? Is the viewForAnnotation method getting called? Maybe the map view's delegate is not set. –  Jul 24 '11 at 17:11
  • 1
    That was the problem, thanks. BTW: can I get a callout with a custom image? – DeShawnT Jul 24 '11 at 17:46
  • Your question and code snippet do not match up. – jarjar Jul 24 '11 at 21:53

1 Answers1

1

To get a callout with a custom image, in the viewForAnnotation delegate method, you can set either the leftCalloutAccessoryView or rightCalloutAccessoryView to your image (although the one on the right is usually used for a disclosure button):

annotationView.canShowCallout = YES;
UIImage *img = [UIImage imageNamed:@"something.png"];
annotationView.leftCalloutAccessoryView = 
    [[[UIImageView alloc] initWithImage:img] autorelease];