6

I have subclassed MKAnnotationView to create an annotation that basically draws a circle around a point on a map view through override of drawRect. The circle draws fine in the following situations (in the simulator):

  • On initial load of the map view
  • On swipe, but only when swipe motion is stopped before touch ends (so that map doesn't "coast" after touch ends)
  • On pinch zoom

The circle will disappear when any of the following actions occur:

  • Swipe where map "coasts" after touch ends
  • Double-tap zoom

The circle will reappear if any of the actions in the "working" group are taken after it has disappeared.

What might cause this? I'm not a draw/display/layout expert (frankly, I'm not an obj C or iPhone expert either).

Here is some slightly simplified code that seems most relevant from my MKAnnotationView subclass:

- (void)drawRect:(CGRect)rect {
    // Drawing code
 [self drawCircleAtPoint:CGPointMake(0,0) withRadius:self.radiusInPixels andColor:self.circleAnnotation.color];
}


- (void)drawCircleAtPoint:(CGPoint)p withRadius:(int)r {
    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    float alpha = 0.75;

    CGContextSetRGBFillColor(contextRef, 255, 0, 0, alpha);
    CGContextSetRGBStrokeColor(contextRef, 255, 0, 0, alpha);

    // Draw a circle (border only)
    CGContextStrokeEllipseInRect(contextRef, CGRectMake(0, 0, 2*r, 2*r));
}
Steve N
  • 2,667
  • 3
  • 30
  • 37

1 Answers1

2

Did you add this method?

- (void)setAnnotation:(id <MKAnnotation>)annotation
{
    [super setAnnotation:annotation];
    [self setNeedsDisplay];
}

This is taken from Apple's sample code app called WeatherMap which was removed from Apple Developer Center, but can be found on github https://github.com/acekiller/iOS-Samples/blob/master/WeatherMap/Classes/WeatherAnnotationView.m

Dunja Lalic
  • 752
  • 13
  • 26
  • The weather annotations in the sample code disappear and re-appear after every drag/ zoom. – Robert Jun 09 '11 at 14:24
  • 1
    That's because they implemeted it that way, see '- (void)mapView:(MKMapView *)map regionDidChangeAnimated:(BOOL)animated' method in 'MapViewController.m' – Dunja Lalic Jun 13 '11 at 13:29
  • Can you please let me know where should I put this code? As your provided link is not available anymore. – NSP Mar 03 '15 at 12:45
  • Sure, it should be in your custom subclass of MKAnnotationView as indicated in the question. Seems Apple has removed their sample code, but I found it on github: https://github.com/acekiller/iOS-Samples/blob/master/WeatherMap/Classes/WeatherAnnotationView.m – Dunja Lalic Apr 07 '15 at 17:32