chespinoza
chespinoza

Reputation: 2658

How to get point coordinates of a modified drawingManager shape? GoogleMaps API v3

I have this DrawingManager Object:

    drawingManager = new google.maps.drawing.DrawingManager({
      drawingMode: google.maps.drawing.OverlayType.POLYGON,
      markerOptions: {
        draggable: true
      },
      polylineOptions: {
        editable: true
      },
      polygonOptions: polyOptions,
      map: map
    });

And when a Polygon is completed I get their coords with:

    google.maps.event.addListener(drawingManager, 'polygoncomplete', function (polygon) {
        var coordinates = (polygon.getPath().getArray());
        console.log(coordinates);
      });

But if I change the polygon using DrawingManager obviously the shape will change, maybe adding more Points..
Then How can I get all Points with their coords after modify it and for example click a button to finish the edition?? Thanks in advance.

Upvotes: 5

Views: 9659

Answers (2)

ChompChomp
ChompChomp

Reputation: 11

Heres our solution for passing the coordinates of a polygon using a for loop using an event listener

google.maps.event.addListener(testMap, 'polygoncomplete', function (polygon) {
    var coordinates = (polygon.getPath());
    console.log(coordinates);

for (let i = 0; i < coordinates.getLength(); i++) 
 {
   const xy = coordinates.getAt(i);
   var contentString = console.log(JSON.stringify({point: xy +[i+1]}))
 }

Upvotes: 1

chespinoza
chespinoza

Reputation: 2658

Ok having the answer on my second code:

var coordinates = (polygon.getPath().getArray());

Finally I got the last array with coordinates calling this code by adding a listener to call a function that get the array:

JS

function getCoordinates() {
    console.log(polygon.getPath().getArray());
}

google.maps.event.addDomListener(document.getElementById('CoordsButton'), 'click', getCoordinates);

HTML

<button id="CoordsButton">Coordinates</button>

Then when the button is clicked now I get the coords...

Thanks anyway

Upvotes: 14

Related Questions