Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getpaths() polygons google maps api

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 += ")";

                    }
        });
like image 277
ddalbus Avatar asked Feb 19 '13 01:02

ddalbus


People also ask

Can you add polygons to Google Maps?

Draw a path or polygonTo make a path or polygon into a 3D object, click Altitude. A "New Path" or "New Polygon" dialog will pop up. You may need to move it out of the way before moving on to the next step. To draw the line or shape you want, click a start point on the map and drag.

How do I create a geofence on Google Maps?

Plotting a Geofence on the map To plot a geofence on the map, all you require are, center coordinates of the circle (latitude, longitude) and radius of the circle. With a GoogleMap object reference, you can use the addCircle() function which expects CircleOptions() object as a parameter.

How do I calculate the area of a polygon in Google Maps?

Right-click on the map at your starting point and choose the Measure distance option. Add points around the location's boundary. Once you close the shape by clicking on the starting point, the Google Maps area calculator will automatically process the area of your shape.


1 Answers

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 += ")";
like image 114
Chad Killingsworth Avatar answered Sep 30 '22 13:09

Chad Killingsworth