2014-10-20 89 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.邊界框不適合封閉一個國家。簡化的多邊形會更好。