4

Is there a way to set the map bounds and actually limit the user to be able to pan only within those boundaries?

The closest I got to what I need is the mapView.fitBounds method but it does not seem to be limiting the panning at all. Am I doing something wrong or is this method not doing what I need?

I'm using SKMaps iOS SDK version 2.5

Thanks!

p4bloch
  • 267
  • 1
  • 7

2 Answers2

4

Here is some code I wrote (based on this answer) to bind the user within a bounding box, defined by a top left coordinate, and a bottom right coordinate.

In my viewDidLoad after initiating the mapView object like so

mapView = [[SKMapView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f,  CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) )];

//Setting the zoom limit, (totally optional)
SKMapZoomLimits zoomLimits;
zoomLimits.mapZoomLimitMin = 15.153500;
zoomLimits.mapZoomLimitMax = 21;
mapView.settings.zoomLimits = zoomLimits;

//Creating our bounding box by specifying a top left point, and a bottom right
CLLocationCoordinate2D topLeftBoundary;
topLeftBoundary.longitude = 21.174489;
topLeftBoundary.latitude = 39.777993;

CLLocationCoordinate2D botRightBoundary;
botRightBoundary.longitude = 21.191678;
botRightBoundary.latitude = 39.765834;

_boundaries = [SKBoundingBox boundingBoxWithTopLeftCoordinate:topLeftBoundary bottomRightCoordinate:botRightBoundary];
}

Then i define the isInBoundingBox method.

-(BOOL) isInBoundingBox: (SKCoordinateRegion)regionBox {
    if (_boundaries.topLeftCoordinate.latitude < regionBox.center.latitude || _boundaries.bottomRightCoordinate.latitude > regionBox.center.latitude ||
    _boundaries.topLeftCoordinate.longitude > regionBox.center.longitude || _boundaries.bottomRightCoordinate.longitude < regionBox.center.longitude)
    {
        return false;    
    }
return true;
}

then on our didChangeToRegion method we do this:

- (void)mapView:(SKMapView*)curMapView didChangeToRegion:(SKCoordinateRegion)region{
    //NSLog(@" @@@@@@@@ did change to ZOOM LEVEL: %f and lat: %f, long: %f",region.zoomLevel, region.center.latitude, region.center.longitude);

    BOOL inBoundingBox = [self isInBoundingBox:region];
    if (inBoundingBox) {
        // if mapRegion is valid save it
        _lastValidRegion = region;
    } else {
        // if mapRegion is invalid reposition the map inside the bounding box
        [mapView setVisibleRegion:_lastValidRegion];
    }
}
Community
  • 1
  • 1
SudoPlz
  • 20,996
  • 12
  • 82
  • 123
0

While there is no direct support for this, you can achieve the same "effect" via an workaround: Restrict the dragging of SKMapView to a certain city

Community
  • 1
  • 1
Ando
  • 11,199
  • 2
  • 30
  • 46