Azure Maps - 多边形边界框

Azure Maps - Polygon bounding box

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

下面的代码 returns undefined 当我访问 Polygon 的 bbox 属性 class:

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)

谁能帮帮我。 谢谢。

特征的 bbox 属性 仅在直接 defined/calculated 时才会定义,通常这会填充在 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