December 15, 2009

MapKit Span Doubling Bug

I am currently working on an application that uses the new MapKit API in OS 3.0. Although the feature set is a bit sparse, I have found it useful. I look forward to future added features to this neat package.

There is one nagging bug, though: sometimes setting the map to a region will end up zoomed out too far. What appears to be happening, and has been confirmed by others in this thread over at stackoverflow, is that the span values are essentially doubled. A doubled span means a greater area is viewed, equating to a zoomed out view.

This bug didn't happen all the time, but recently it now happens to me ALL the time on the simulator. Others say the bug persists on the actual device as well.

Until Apple fixes this bug, I needed to provide a fix to get around this annoying defect. The error seems to be introduced when assigning the region to the map:

[self.mapView setRegion:myRegion animated:YES];


The hack is to look at the map region after making the assignment. If the span does not equal what you set it to, then divide the original span values by 2.0 and try it again. Since the MapKit code is doubling the values, we will halve the values beforehand to get to the value we want. Idiotic hack, for sure, but gets the job done:

[self.mapView setRegion:myRegion animated:YES];

// did the span get screwed up?
if(self.mapView.region.span.latitudeDelta != myRegion.span.latitudeDelta ) {
    NSLog(@"Span got doubled, adjusting");


    // hack to fix span doubling bug
    MKCoordinateRegion temp = MKCoordinateRegionMake(myRegion.center
            MKCoordinateSpanMake(myRegion.span.latitudeDelta/2.0,
                                             myRegion.span.longitudeDelta/2.0));
    [self.mapView setRegion:temp animated:YES];
}

The nice thing about this hack is that it only takes affect if the bug appears. Should you forget about this hack and Apple has long since fixed the problem, the hack is harmless.

1 comment:

  1. I observe the same issue, however according to the test MapKit is not dobling span, it sets the region returned by regionThatFits. So this looks like an Apple issue.

    ReplyDelete