限制SKMapView拖动到某个城市
Restrict the dragging of SKMapView to a certain city
我正在尝试限制将 SKMapView
拖动到某个城市。
这是我尝试过的方法,我浏览了文档参考和开源代码。好像 SKBoundingBox
那样做。
但是这段代码不起作用。它仍然允许用户到处平移。
SKBoundingBox *boundingBox = [SKBoundingBox boundingBoxWithTopLeftCoordinate:topLeftCoordinate bottomRightCoordinate:bottomRightCoordinate];
虽然没有对此的集成支持,但您可以做一个解决方法。
here 中的文件应替换 Public SDK 演示应用程序中的文件以查看解决方法 (MapDisplayViewController.m)。
思路是可以从我们提供的Maps.json中得到一个city/country的bounding box信息。然后你需要实现如下回调:
- (void)mapView:(SKMapView *)mapView didChangeToRegion:(SKCoordinateRegion)region {
if (self.bbox) {
if ([self.bbox containsLocation:region.center]) {
self.previousRegion = region;
} else {
[self.mapView animateToLocation:self.previousRegion.center withDuration:0.2];
}
}
}
此实现可确保当用户平移出 city/country 的边界框时,他将被移回边界框包含的先前区域。
缩放级别限制: 可以通过 SKMapsSettings 的 zoomLimits 属性 限制缩放级别:
SKMapZoomLimits zoomLimits;
zoomLimits.mapZoomLimitMin = 5.0f;
zoomLimits.mapZoomLimitMax = 14.0f;
self.mapView.settings.zoomLimits = zoomLimits;
我正在尝试限制将 SKMapView
拖动到某个城市。
这是我尝试过的方法,我浏览了文档参考和开源代码。好像 SKBoundingBox
那样做。
但是这段代码不起作用。它仍然允许用户到处平移。
SKBoundingBox *boundingBox = [SKBoundingBox boundingBoxWithTopLeftCoordinate:topLeftCoordinate bottomRightCoordinate:bottomRightCoordinate];
虽然没有对此的集成支持,但您可以做一个解决方法。
here 中的文件应替换 Public SDK 演示应用程序中的文件以查看解决方法 (MapDisplayViewController.m)。
思路是可以从我们提供的Maps.json中得到一个city/country的bounding box信息。然后你需要实现如下回调:
- (void)mapView:(SKMapView *)mapView didChangeToRegion:(SKCoordinateRegion)region {
if (self.bbox) {
if ([self.bbox containsLocation:region.center]) {
self.previousRegion = region;
} else {
[self.mapView animateToLocation:self.previousRegion.center withDuration:0.2];
}
}
}
此实现可确保当用户平移出 city/country 的边界框时,他将被移回边界框包含的先前区域。
缩放级别限制: 可以通过 SKMapsSettings 的 zoomLimits 属性 限制缩放级别:
SKMapZoomLimits zoomLimits;
zoomLimits.mapZoomLimitMin = 5.0f;
zoomLimits.mapZoomLimitMax = 14.0f;
self.mapView.settings.zoomLimits = zoomLimits;