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;
}
3
  • 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. Commented Jul 24, 2011 at 17:11
  • 1
    That was the problem, thanks. BTW: can I get a callout with a custom image? Commented Jul 24, 2011 at 17:46
  • Your question and code snippet do not match up. Commented Jul 24, 2011 at 21:53

1 Answer 1

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];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.