2014-10-20 78 views
2

我想为SKMapView设置一个“容器边界框”来禁止用户进行导航,比方说,例如,离开德国的边界框(我有坐标的坐标所需的容器)在SKMapView上设置容器边界框

我想我已经用

mapView:didChangeToRegion: 

mapView:didStartRegionChangeFromRegion: 

但我不能让它开始工作以前visibleRegion新visibleRegion比较。

关于如何管理它的任何想法?

感谢您的帮助

回答

1

这是你如何可以通过只执行MapView的做到这一点:didChangeToRegion:

if (![self.bbox containsLocation:region.center] || region.zoomLevel < self.minZoom) { 
    SKCoordinateRegion allowedRegion = region; 
    if (region.center.latitude > self.bbox.topLeftCoordinate.latitude) { 
     allowedRegion.center.latitude = self.bbox.topLeftCoordinate.latitude; 
    } else if (region.center.latitude < self.bbox.bottomRightCoordinate.latitude) { 
     allowedRegion.center.latitude = self.bbox.bottomRightCoordinate.latitude; 
    } 

    if (region.center.longitude > self.bbox.bottomRightCoordinate.longitude) { 
     allowedRegion.center.longitude = self.bbox.bottomRightCoordinate.longitude; 
    } else if (region.center.longitude < self.bbox.topLeftCoordinate.longitude) { 
     allowedRegion.center.longitude = self.bbox.topLeftCoordinate.longitude; 
    } 

    if (region.zoomLevel < self.minZoom) { 
     allowedRegion.zoomLevel = self.minZoom; 
    } 

    mapView.visibleRegion = allowedRegion; 

} 

哪里self.bbox是你想要让边框的SKBoundingBox。 和self.minZoom是允许的最小缩放级别。

虽然这并不理想,但出于以下两个原因: 1.当您尝试跨越边界框边界时,地图有点来回摆动。 2.边界框不适合封闭一个国家。简化的多边形会更好。