ManojAnavatti
ManojAnavatti

Reputation: 644

Azure Maps - Polygon bounding box

I am using Azure Maps and the javascript atlas library: https://learn.microsoft.com/en-us/javascript/api/azure-maps-control/atlas?view=azure-maps-typescript-latest

Below code returns undefined when i access bbox property of Polygon 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. 

Code which works is:

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

I need to calculate centroid from convex hull using:

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

Can anyone please help me. Thanks.

Upvotes: 1

Views: 1401

Answers (1)

rbrundritt
rbrundritt

Reputation: 17954

The bbox property of a feature is only defined if it was defined/calculated directly, often this is populated in GeoJSON files and thus would be populated when read in. By default the map does not populate this field if it isn't already populated as it would mean a lot of unnecessary calculations in majority of apps.

For your scenario you would do this:

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

Here is a similar sample: https://azuremapscodesamples.azurewebsites.net/index.html?sample=Polygon%20labels%20-%20calculated

If you are looking to place a label on the center of the polygon, you might also want to consider this approach: https://azuremapscodesamples.azurewebsites.net/index.html?sample=Polygon%20labels%20-%20symbol%20layer

Upvotes: 3

Related Questions