Tag Archives: iPhone

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 add 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 interested in hearing if others have encountered this problem and how you solved it.

iPhone Utilities for your Mac or PC

I stumbled upon a couple of really good iPhone utilities for the Mac and PC from MacroplantiPhone Explorer and Phone Disk.  These apps allow you to see what is on your iPhone, iPod Touch, or iPad – pretty cool.  They’re not quite as useful if the device isn’t jail-broken, but they’re still useful.

iPhoneExplorer PhoneDisk

For example, they will both allow you view the app and media files that are on your device.  They both allow you to copy files to and from your device as well.  iPhone Explorer allows you to see all the files related to your apps as well while Phone Disk allows you to delete files on your device.  iPhone Explorer provides a custom UI for viewing the contents whereas Phone Disk mounts the device as a disk, just like a USB flash drive.

Macroplant provides iPhone Explorer free of charge, while normally they charge for Phone Disk.  Until December 2010, however, they are offering it for free.

Resources