I'm working on an iPhone app that shows a map with multiple circle overlays on certain locations. I'm running into serious memory issues and crashes when I add more than 6 circles and I zoom out far enough that they are all visible. When I zoom in such that only 2 circles are visible, all is fine. When I remove the MKOverlays, everything works fine.
Anyone who recognizes this behavior?
Code that creates the overlays. I store the overlays in a NSMutableDictionary for future reference (to be able to remove them from the map and to prevent double overlays)
- (void)updateMarkersForZones:(NSArray *)zones {
NSLog(@"MapViewController: Update Markers");
// For each zone, show a marker
for (Zone* zone in zones) {
NSString *keyMarker = [NSString stringWithFormat:@"%d-marker", zone.id];
MKCircle *circle = [overlayCache objectForKey:keyMarker];
if (circle == nil) {
// draw the radius circle for the marker
double radius = MAX(zone.markerRadius * 1.0, 1.0);
circle = [MKCircle circleWithCenterCoordinate:zone.location radius:radius];
[mapView addOverlay:circle];
// store the circle in a cache for future reference
[overlayCache setObject:circle forKey:keyMarker];
}
}
}
Code that makes the overlay views
#pragma mark -
#pragma mark MKMapViewDelegate
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay{
MKCircleView *circleView = [[[MKCircleView alloc] initWithCircle:overlay] autorelease];
circleView.lineWidth = 1.0;
circleView.strokeColor = [UIColor redColor];
return circleView;
}
Code that releases the overlay cache
- (void)dealloc {
[overlayCache release];
[mapView release];
[super dealloc];
}