1
我有一個NTVectorElements數組,我如何設置地圖邊界以便它適合屏幕上的每個元素?我看到moveToFitBounds函數,但我不知道如何實現它。你有一個例子嗎?將地圖擬合到Carto mobile SDK中的對象
我有一個NTVectorElements數組,我如何設置地圖邊界以便它適合屏幕上的每個元素?我看到moveToFitBounds函數,但我不知道如何實現它。你有一個例子嗎?將地圖擬合到Carto mobile SDK中的對象
我以前處理過這個問題。搞清楚並不是一項簡單的任務,但我可以提供解決此問題的示例代碼段。
您需要提供屏幕邊界以及地圖邊界,這意味着繪製邊界框所需的最小和最大位置。
NTVectorElements沒有方法立刻得到對象範圍,你需要經過所有的數組中的元素,找到他們的幾何形狀的全球最大和最小
這裏是適合地圖上的一個片段當前加載的網站,你應該只需要很少的修改,以滿足您的使用情況:
-(void)fitMapToCurrentlyLoadedSites {
int siteCount = (int)[_sitesOrderArray count];
if (siteCount > 0) {
if (siteCount == 1) {
//zoom in on single site
GenericMapMarker *siteMarker = [_sitesOrderArray objectAtIndex:0];
NTMapPos *sitePosition = [CommonFunctions getMapPosFromCoordinate:_ntMapView coordinate:siteMarker.coordinate];
[_ntMapView setFocusPos:sitePosition durationSeconds:0];
[_ntMapView setZoom:15.0 durationSeconds:0];
} else {
//create vector of multiple sites
NTMapPosVector* posVector = [[NTMapPosVector alloc] init];
for (int i = 0; i < siteCount; i++) {
GenericMapMarker *siteMarker = [_sitesOrderArray objectAtIndex:i];
//get mapPos from coordinate
NTMapPos *mapPos = [CommonFunctions getMapPosFromCoordinate:_ntMapView coordinate:siteMarker.coordinate];
[posVector add:mapPos];
}
//create envelope of vectors
NTMapEnvelope *envelope = [[NTMapEnvelope alloc] initWithConvexHull:posVector];
//get mapBounds of envelope
NTMapBounds *bounds = [envelope getBounds];
[_ntMapView moveToFitBounds:bounds screenBounds:[self findScreenBounds] integerZoom:TRUE durationSeconds:1.0f];
}
}
}
,並找到屏幕邊界:
-(NTScreenBounds *)findScreenBounds {
float screenWidth = self.view.frame.size.width;
float screenHeight = self.view.frame.size.height;
NTScreenPos *minScreenPos = [[NTScreenPos alloc] initWithX:0.0 y:0.0];
NTScreenPos *maxScreenPos = [[NTScreenPos alloc] initWithX:screenWidth y:screenHeight];
return [[NTScreenBounds alloc] initWithMin:minScreenPos max:maxScreenPos];
}