将地图限制在一定范围内?

有没有一种方法来设置地图边界,并实际限制用户只能在这些边界内平移?

我得到我所需要的最接近的是mapView.fitBounds方法,但它似乎并没有限制平移。 我做错了什么,或者这种方法没有做我所需要的?

我正在使用SKMaps iOS SDK版本2.5

谢谢!

这里是我写的一些代码(基于这个答案)绑定用户在一个边界框内,由左上angular坐标和右下angular坐标定义。

在我的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]; } } 

虽然没有直接的支持,但可以通过一个解决方法达到相同的“效果”: 限制将SKMapView拖到某个城市