Limit a map to certain bounds?

最后都变了- 提交于 2019-12-10 16:12:05

问题


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!


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/32031325/limit-a-map-to-certain-bounds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!