Bug in removeOverlay: in MKMapView
I’ve been developing an iPhone app on my job at INRIX that uses MapKit and have run into a number of interesting issues. Â One in particular baffled me for some time, and it turns out to be (or at least appear to be) a bug in MKMapView.
The app displays traffic on a map, and on iOS4 it uses an overlay to display the traffic tiles. Â As the user moves around or zooms in and out, the traffic tile image must be updated, which means removing the overlay and adding a new one. Â This appears to work at most zoom levels until you start zooming in closer. Â In those cases, the traffic tile would appear as a big blob – very ugly.
Searching around the net I found a few people that had experienced the same problem. Â The work-around is to ad a dummy transparent overly. Â Sounds silly, but it works.
Here are some code snippets that work for me:
- (void) loadView
{
// ...
_mapView = [[MKMapView alloc] init];
_mapView.delegate = self;
// ...
[self.view addSubview: _mapView];
// ...
BOOLÂ isOS4orAbove = ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0f);
if (isOS4orAbove)
{
// Add a dummy overlay to fix a bug in MKMapView where overlays don't get removed properly.
// https://devforums.apple.com/message/307581
MKPolyline * dummyOverlay = [[MKPolyline alloc] init];
[_mapView addOverlay: dummyOverlay];
[dummyOverlay release];
}
// ...
}
- (MKOverlayView *) mapView: (MKMapView *) mapView viewForOverlay: (id <MKOverlay>) overlay
{
if ([overlay isKindOfClass: [TileOverlay class]])
{
// Return view for traffic tile overlay.
}
else if ([overlay isKindOfClass: [MKPolyline class]])
{
// This is for a dummy overlay to work around a problem with overlays
// not getting removed by the map view even though we asked for it to
// be removed.
MKOverlayView * dummyView = [[[MKOverlayView alloc] initWithOverlay: overlay] autorelease];
dummyView.alpha = 0.0;
return dummyView;
}
else
{
return nil;
}
}
The forum post that led me to this solution can be found here. Â This appears to be a bug in MapKit. Â I’d be interesting in hearing if others have encountered this problem and how you solved it.

Leave your comment...