Refresh MKAnnotationView on a MKMapView

This worked for me

#import "EGOImageView.h"

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    //ENTER_METHOD;    
    if([annotation isKindOfClass:[MKUserLocation class]]) return nil;  
    MKPinAnnotationView *annView;

    static NSString *reuseIdentifier = @"reusedAnnView";
    annView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5.0f, 0.0f);
    annView.opaque = NO;
    annView.image = [[UIImage imageNamed:@"tempImage.png"] retain];

    YourModel *p = annotation; // make this conform to <MKAnnotation>
    EGOImageView *egoIV = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"tempImage.png"]];
    [egoIV setDelegate:self];
    egoIV.imageURL = [NSURL URLWithString:p.theUrlToDownloadFrom];
    [annView addSubview:egoIV];
    [egoIV release];


    return [annView autorelease];
}

This is the only way I've found to force redraw of MKAnnotationViews:

// force update of map view (otherwise annotation views won't redraw)

CLLocationCoordinate2D center = mapView.centerCoordinate;

mapView.centerCoordinate = center;

Seems useless, but it works.


I haven't tried it myself, but it might work:
Get a view for annotation using MKMapView - (MKAnnotationView *)viewForAnnotation:(id <MKAnnotation>)annotation method and set new image to it.

Edit: Tried to do that myself and this approach worked fine for me:

//  Function where you got new image and want to set it to annotation view
for (MyAnnotationType* myAnnot in myAnnotationContainer){
    MKAnnotationView* aView = [mapView viewForAnnotation: myAnnot];
    aView.image = [UIImage imageNamed:@"myJustDownloadedImage.png"];
}

After calling this method all annotations images were updated.


I found only one reliable way to update annotation, others methods didn't work for me: just removing and adding annotation again:

id <MKAnnotation> annotation;

// ...

[mapView removeAnnotation:annotation];
[mapView addAnnotation:annotation];

This code will make -[mapView:viewForAnnotation:] to be called again and so your annotation view will be created again (or maybe reused if you use dequeueReusableAnnotationViewWithIdentifier) with correct data.