How to get center point of HTML 5 map area? On this web page http://webven.ru/portfolio/istra/page.html there are polygons, with many coordinates. How can I get the center point?
I have tried below code:
function calculateCenterPoint(areas) {
var maxX = 0,
minX = Infinity,
maxY = 0,
minY = Infinity;
Array.prototype.forEach.call(areas, function (e) {
var i = 0,
coords = e.getAttribute('coords').split(',');
while (i < coords.length) {
var x = parseInt(coords[i++],10),
y = parseInt(coords[i++],10);
if (x < minX) minX = x;
else if (x > maxX) maxX = x;
if (y < minY) minY = y;
else if (y > maxY) maxY = y;
}
});
return {
x: minX + (maxX - minX) / 2,
y: minY + (maxY - minY) / 2
};}
But it does not work, for all of the area.
Remove the else
in else if (x > maxX)
and else if (y > maxY)
.
The problem is that in the first iteration of the loop, both conditions can (and will) be true.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.