Search code examples
iphonemkmapviewmkpinannotationview

Add image behind MKPinAnnotationView


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


Solution

  • 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.