Search code examples
iphonemkmapviewmkannotationview

Redraw MKAnnotationView


Depends of zoom level of MKMapView I need to redraw my custom MKAnnotationView. As far, as I understood I must to do it in next method of map controller

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

Removing and then adding annotation to MKMapView here force blinking of AnnotationView. How can I do this in a right way?


Solution

  • You don't need to remove it and add it back. Just make a change on the custom annotation view and call setNeedsDisplay. Example:

    @interface AnnotationClusterView : MKAnnotationView {
    @property (nonatomic, assign) int badgeNumber;
    }
    @implementation AnnotationClusterView
    @synthesize badgeNumber;
    // ...
    - (void)drawRect:(CGRect)rect {
        NSString *string = [NSString stringWithFormat:@"%d",self.badgeNumber];
        [string drawInRect:stringRect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:13.0]];
    }
    @end
    

    When zoom changes, get a reference to the MKAnnotationView, set a different badgeNumber, and ask for a redraw calling [myView setNeedsDisplay];. You can do the same for images.