MKMapView range doubling error
Setting the area in MKMapView sometimes results in a doubling of the range. This error probably appears at an early stage of card initialization. Although I have been told elsewhere that I was unable to find an existing workaround, so I am posting a fix. It relies on the regionThatFits method also throwing an error. I am working with iPhone OS 3.12 but a bug was reported in 3.0. This code is in the UIViewController that contains your MKMapView:
- (BOOL)doubleSpanBugDetected:(MKCoordinateRegion)region fittedRegion:(MKCoordinateRegion)fitted
{
float latRatio = fitted.span.latitudeDelta / region.span.latitudeDelta;
float lonRatio = fitted.span.longitudeDelta / region.span.longitudeDelta;
BOOL latDoubled = (latRatio > 1.8 && latRatio < 2.2); // within 10% of x2
BOOL lonDoubled = (lonRatio > 1.8 && lonRatio < 2.2); // within 10% of x2
return latDoubled && lonDoubled;
}
- (void)setRegion:(MKCoordinateRegion)region animated:(BOOL)animated
{
//fixes setRegion span doubling bug
// see: http://osmorphis.blogspot.com/2009/12/mapkit-span-doubling-bug.html
// see: http://www.iphonedevsdk.com/forum/iphone-sdk-development/15810-mkmapview-needs-time-think.html
MKCoordinateRegion fitted = [self.mapView regionThatFits:region];
if ([self doubleSpanBugDetected:region fittedRegion:fitted]) {
MKCoordinateSpan span = MKCoordinateSpanMake(fitted.span.latitudeDelta/2.0, fitted.span.longitudeDelta/2.0);
MKCoordinateRegion regionHack = MKCoordinateRegionMake(fitted.center, span);
[self.mapView setRegion:regionHack animated:animated];
} else {
[self.mapView setRegion:fitted animated:animated];
}
}
+2
a source to share
No one has answered this question yet
Check out similar questions: