Getpaths () polygons google maps api

I am trying to get the latlng coordinates of both a polyline and a polygon. After I finish drawing any object, I would like to save latlng in the database, but for now I'm just trying to show latlng in a text box. I made it easy enough for markers, rectangles and circles, but the terminology for polylines and polygons puzzles me. When I finish drawing, I use addDomListener (drawingManager, "polygoncomplete", ... for polynomials, and then iterate over all the polygons that I drew. For each polygon, I then repeat its array of coordinates. I searched this forum database and tried an example Bermuda Triangle on Google Docs Page: An Example of Bermuda I read the documentation many times and simply could not see what was missing. Any help is appreciated.

//Save polygons data to text area
                var polygons = [];

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

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

                    }
        });
+5
source share
1 answer

The Google Maps Polygon class returns an MVCArray . You need to use the forEach MVCArray method to scroll it.

var polygonBounds = polygons[i].getPath();
// Iterate over the polygonBounds vertices.
polygonBounds.forEach(function(xy, i) {
  contentString += '<br>' + 'Coordinate: ' + i + '<br>' + xy.lat() +',' + xy.lng();
});
document.getElementById("savedatapolygon").value += "polygon(";
document.getElementById("savedatapolygon").value += contentString;
document.getElementById("savedatapolygon").value += ")";
+16
source

All Articles