0

I have this code where when the user clicks on the map like 3 times a polygon is created as well as a marker in each nodal point. Anyone knows how I could calculate the middle point of the polygon and drop a marker there as well ? thanks. here is my code.

      var poly;
      var map;

     function initialize()
     {
 map = new google.maps.Map(document.getElementById('map'),
    {
    zoom: 14,
    center: new google.maps.LatLng(12.303022,76.644917),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

poly = new google.maps.Polygon(
{
    strokeColor: '#000000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: "#000000",
    fillOpacity: 0.26
  });
  poly.setMap(map);

  google.maps.event.addListener(map, 'click', addLatLng);
}

   function addLatLng(event)
    {
var path = poly.getPath();
path.push(event.latLng);

var marker = new google.maps.Marker(
{
    position: event.latLng,
    title: '#' + path.getLength(),
    map: map
});
  }
user3449550
  • 123
  • 5
  • 17
  • It depends what do you mean by "middle point" of polygon. One option is to calculate the center of the mass. In order to do this you just have to calculate average of latitude across all points, and average longitude for all points. – Piotr Zurkowski Mar 23 '14 at 14:53
  • It would also be simpler for a triangle (3 points) than for an arbitrary polygon. Is that all you need? If not search for algorithms (they are out there). – geocodezip Mar 23 '14 at 15:55
  • possible duplicate of [How to get the center of a polygon in google maps v3?](http://stackoverflow.com/questions/3081021/how-to-get-the-center-of-a-polygon-in-google-maps-v3) – Anto Jurković Mar 23 '14 at 16:13

0 Answers0