Azure地图-多边形边界框

时间:2020-04-18 14:51:02

标签: azure-maps

我正在使用Azure Maps和javascript地图集库: https://docs.microsoft.com/en-us/javascript/api/azure-maps-control/atlas?view=azure-maps-typescript-latest

当我访问Polygon类的bbox属性时,以下代码返回undefined

var hull = atlas.math.getConvexHull(positions);
var boundingBox = hull.bbox //returns undefined.

var polygon = new atlas.data.Polygon(positions);
var bBox = polygon.bbox //returns undefined even here. 

有效的代码是:

var boundingBox = atlas.data.BoundingBox.fromPositions(positions); //Works fine. 

我需要使用以下方法从凸包计算质心:

var centroid = atlas.data.BoundingBox.getCenter(hull.bbox)

任何人都可以帮助我。 谢谢。

1 个答案:

答案 0 :(得分:1)

仅当直接定义/计算功能的bbox属性时,才定义该属性,通常将其填充在GeoJSON文件中,因此在读入时将被填充。默认情况下,地图未填充此字段(如果未将其填充)已经填充,因为这意味着大多数应用程序中会进行许多不必要的计算。

对于您的情况,您可以这样做:

var hull = atlas.math.getConvexHull(positions);
var boundingBox = atlas.data.BoundingBox.fromData(hull);
var centroid = atlas.data.BoundingBox.getCenter(boundingBox);

这是一个类似的示例:https://azuremapscodesamples.azurewebsites.net/index.html?sample=Polygon%20labels%20-%20calculated

如果要在多边形中心放置标签,则可能还需要考虑以下方法:https://azuremapscodesamples.azurewebsites.net/index.html?sample=Polygon%20labels%20-%20symbol%20layer