将地图限制在特定范围内?
Limit a map to certain bounds?
有没有办法设置地图边界并实际限制用户只能在这些边界内平移?
最接近我需要的是 mapView.fitBounds
方法,但它似乎根本没有限制平移。我做错了什么或者这个方法没有做我需要的吗?
我正在使用 SKMaps iOS SDK 版本 2.5
谢谢!
虽然没有对此的直接支持,但您可以通过解决方法实现相同的 "effect":
这是我编写的一些代码 (based on this answer) 以将用户绑定在由左上角坐标和右下角坐标定义的边界框内。
在我的 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];
}
}
有没有办法设置地图边界并实际限制用户只能在这些边界内平移?
最接近我需要的是 mapView.fitBounds
方法,但它似乎根本没有限制平移。我做错了什么或者这个方法没有做我需要的吗?
我正在使用 SKMaps iOS SDK 版本 2.5
谢谢!
虽然没有对此的直接支持,但您可以通过解决方法实现相同的 "effect":
这是我编写的一些代码 (based on this answer) 以将用户绑定在由左上角坐标和右下角坐标定义的边界框内。
在我的 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];
}
}