0

I have been toying around with Google Maps Drawing tools v3. If a user creates a new polygon, I used this example to be able to track the location of the polygons:

getpaths() polygons google maps api

Code:

var polygons = [];

       google.maps.event.addDomListener(drawingManager, 'polygoncomplete', function (polygon) {
           polygons.push(polygon);
           console.log("polygon complete");
       });

       google.maps.event.addDomListener(savebutton, 'click', function () {
           document.getElementById("savedatapolygon").value = "";
           console.log(polygons.length);
           for (var i = 0; i < polygons.length; i++) {
               var polygonBounds = polygons[i].getPath();
               // Iterate over the polygonBounds vertices.
               var PolyNum = i
               console.log(PolyNum);
               polygonBounds.forEach(function (xy, i) {
                   console.log('Coordinate: ' + PolyNum + ':' + i + '<br>' + xy.lat() + ',' + xy.lng());
               });
           }
       });

This is fine for when a user creates a Polygon, but what if they delete the Polygon? The data is still stored in the array. I can't seem to find a method to either track which array was deleted or have it iterate through all shapes at the end when the user clicks save. Looking through the console, I can see that there might be a unique id assigned to each shape called __gm_id:, but i'm not sure if this would be the best way to track it.

The other strange thing is that when you edit a shape, those changes are somehow getting updated to the polygon array even though there is nothing in the code stating that it should.

Community
  • 1
  • 1
sfreelander
  • 161
  • 1
  • 2
  • 11

1 Answers1

1

Google examples show the way to delete an object is to use:

selectedShape.setMap(null);

I found it easier to track the the polygon end result by making the visibility false instead of deleting:

selectedShape.setOptions({ visible: false });

That way, when reading the array you can check if it is invisible:

polygons[i].getVisible()

That in the codebehind you can deal with the data however you want when saving the coordinates. You will get everything that was made, but can choose to only save the shapes that are visible.

sfreelander
  • 161
  • 1
  • 2
  • 11