使地图适合 Carto 移动 SDK 中的对象

Fit map to objects in Carto mobile SDK

我有一个 NTVectorElements 数组,如何设置地图边界以使其适合屏幕上的每个元素?我看到了 moveToFitBounds 函数,但我不确定如何实现它。你有例子吗?

我以前处理过这个问题。弄清楚它不是一件容易的事,但我可以提供一个示例片段来解决这个问题。

您需要提供屏幕边界和地图边界,即绘制边界框所需的最小和最大位置。

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];
}