Limit map to specific borders?

Is there a way to set the boundaries of the maps and actually limit the user to the ability to pan only within these boundaries?

The closest I got what I need is a method mapView.fitBounds, but it does not seem to limit the panning. Am I doing something wrong or is this method not doing what I need?

I am using SKKPS iOS SDK version 2.5

Thanks!

+4
source share
2 answers

Here is the code I wrote (based on this answer) to link the user in a bounding box defined by the upper left coordinate and the lower right coordinate.

viewDidLoad mapView

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

isInBoundingBox.

-(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;
}

didChangeToRegion :

- (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];
    }
}
+4

, "" : SKMapView

0

All Articles