Created
May 27, 2014 14:18
-
-
Save Proper-Job/e11cca67e77285566d4d to your computer and use it in GitHub Desktop.
Restrict MKMapView to specific region
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated | |
{ | |
if (!self.manuallyChangingMap) { | |
BOOL updateRegion = NO; | |
MKCoordinateRegion restrictedRegion = [self restrictedRegion]; | |
if ((mapView.region.span.latitudeDelta > restrictedRegion.span.latitudeDelta * 4) || (mapView.region.span.longitudeDelta > restrictedRegion.span.longitudeDelta * 4) ) { | |
updateRegion = YES; | |
} | |
if (fabs(mapView.region.center.latitude - restrictedRegion.center.latitude) > restrictedRegion.span.latitudeDelta) { | |
updateRegion = YES; | |
} | |
if (fabs(mapView.region.center.longitude - restrictedRegion.center.longitude) > restrictedRegion.span.longitudeDelta) { | |
updateRegion = YES; | |
} | |
if (updateRegion) { | |
self.manuallyChangingMap = YES; | |
[mapView setRegion:region animated:YES]; | |
self.manuallyChangingMap = NO; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried this, and it doesn't work, as
self.manuallyChangingMap = NO;
is called before- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
which is caused by[mapView setRegion:region animated:YES];
.