Z
Z
zlodiak2019-01-14 17:51:33
JavaScript
zlodiak, 2019-01-14 17:51:33

How to calculate the center of a polygon?

I displayed a conditional contour on the map and now I would like to display a balloon in it. Creating a balloon is not a problem, not placing it in the center of a polygon is a problem.
JSFIDDLE
There are quite complex formulas in mathematics for calculating the center of mass of a polygon, I don't know how to use them. but Yandex api has poly.geometry.getBounds(), which takes the largest possible contour and finds the center. I also can’t apply it because I created the polygon not in the standard way ( new ymaps.Polygon()), but through map.geoObjects.add(objectManager);
Please help me get the coordinates of the center of the polygon.
If needed, here is a fiddle that demonstrates getBounds().

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2019-01-14
@zlodiak

var myPoly = new ymaps.Polygon()
var bounds = myPoly.geometry.getBounds()
further on the second fiddle consider center type
For the fiddle from the question: https://jsfiddle.net/d1hmbgfr/
see the docs: https://tech .yandex.ru/maps/doc/jsapi/2.1/ref/ref...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question