Skip to content

Instantly share code, notes, and snippets.

@Proper-Job
Created May 27, 2014 14:18
Show Gist options
  • Save Proper-Job/e11cca67e77285566d4d to your computer and use it in GitHub Desktop.
Save Proper-Job/e11cca67e77285566d4d to your computer and use it in GitHub Desktop.
Restrict MKMapView to specific region
- (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;
}
}
}
@hongchaozhang
Copy link

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];.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment