Add image behind MKPinAnnotationView

Adolfo picture Adolfo · Oct 2, 2009 · Viewed 14.1k times · Source

I'm trying to add an image behind a MKPinAnnotationView. Seems like it should be rather easy to just do this in here:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
 for (MKAnnotationView *aView in views)
  [[aView superview] addSubview:imageView];
}

But the problem I am having in doing that is that the child subview to the pin will render on top of it not BEHIND it.

I have also tried:

 for (MKAnnotationView *aView in views)
  [[aView superview] insertSubview:imageView atIndex:1];

The problem with this is that, while it IS behind the pin, as soon as the map is repositioned, the image floats off the screen.

Any suggestions? Thanks

Answer

Adolfo picture Adolfo · Oct 4, 2009

Thanks for the input, here's basically what I ended up doing without subclassing:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation {

    MKAnnotationView *annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];

    UIImage *image = [UIImage imageNamed:@"image.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [annView addSubview:imageView];
    [imageView release];

    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];     
    [annView addSubview:pinView];
    [pinView release];

    return annView;
}

I only needed one pin, so I set reuseIdentifier to nil.