I want to find the center of a polygon in google map, I tried several solutions however none of them worked correctly for right triangle polygon here is example code:
var path = new Array(new google.maps.LatLng(1, 1), new google.maps.LatLng(1, 10), new google.maps.LatLng(10, 10), new google.maps.LatLng(1, 1));
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(40, 9),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
var polygon = new google.maps.Polygon({
path: path,
map: map
});
var bounds = new google.maps.LatLngBounds();
for (var i=0; i<polygon.getPath().length; i++) {
var point = new google.maps.LatLng(path[i].lat(), path[i].lng());
bounds.extend(point);
}
map.fitBounds(bounds);
const marker = new google.maps.Marker({
position: {
lat: bounds.getCenter().lat(),
lng: bounds.getCenter().lng()
},
map: this.map,
title: "Hello World!"
});
marker.setMap(map);
I used google.maps.LatLngBounds();
for finding cneter of the triangle and mark the center in the map however the result center is not actually true center of the triangle, jsfiddle example of this code.
I used another solution which is discussed here for finding the center of the polygon but that won't work either so is there any other solution for finding an accurate center of polygons regardless of polygon type?