5

I am attempting to retrieve the latlng coordinates of both a polyline and polygon. After I complete the drawing of either object I would like to store the latlng in a database, but for now I am merely trying to show the latlng in a textarea. I have done this easily enough for markers, rectangles, and circles, but the terminology for polylines and polygons is baffling me. When I complete the drawing i use an addDomListener(drawingManager, 'polygoncomplete', ... for polyons and then iterate through all the polygons i have drawn. For each polygon I then iterate through its array of coords. I have searched this forums database and tried the Bermuda triangle example on the Google documentation page. Bermuda Example I have read over the documentation many times and just cant see what i am 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 += ")";

                    }
        });
Kara
  • 6,115
  • 16
  • 50
  • 57
ddalbus
  • 53
  • 1
  • 1
  • 3
  • What javascript errors do you get? [This example](http://www.geocodezip.com/blitz-gmap-editor/test5.html) might help you, it exports all the objects from the DrawingManager as either JSON or KML. – geocodezip Feb 19 '13 at 01:40
  • perfect! this will save me alot of time. i was using textpad at work because thats all i have available to me and i can not download anything else so i cant track any errors :/ – ddalbus Feb 19 '13 at 16:55
  • Most browsers have a javascript console that reports errors, it is simpler with a debugger, but seeing the reported errors is sometimes all you need. – geocodezip Feb 19 '13 at 16:57

1 Answers1

19

The Google Maps Polygon class returns a MVCArray. You need to use the forEach method of the MVCArray to loop through 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 += ")";
Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57