我有一个 NTVectorElements 数组,如何设置地图边界以使其适合屏幕上的每个元素?我看到了 moveToFitBounds 函数,但我不确定如何实现它。你有一个例子吗?
问问题
178 次
1 回答
2
我以前处理过这个问题。弄清楚它不是一项简单的任务,但我可以提供一个示例片段来解决这个问题。
您需要提供屏幕边界和地图边界,即绘制边界框所需的最小和最大位置。
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];
}
于 2017-09-28T14:15:53.120 回答