Question

I have drawn polygon using google map api
now I want to save vertices of polygon in database

my code for polygon is very lengthy as it is fetching from dabase and edit/delete mode etc

var paths=[
    new G.LatLng(34.051979,71.987419),
    new G.LatLng(34.051966,71.987757),
    new G.LatLng(34.051877,71.987762),
    new G.LatLng(34.051886,71.987413),
    new G.LatLng(34.051979,71.987419)
];
poly = new G.Polygon({
   clickable: false,
   paths: paths,
   map: map
});

and I have created a button to edit map and set map on set map I want to save vertices in DB by sending getpath() through ajax

$("#toggleMapEdit").click(function(){
    if($(this).html()=="Edit Map")
    {
        $(this).html("Set Map");
        poly.setEditable(true);
    }
    else
    {

        var polygonBounds = poly.getPath(); //This returns MVCArray
        /*  I have debugged this array, it containg alot of other things other than vertices
        *I want to convert this array into javascript array or json or anything else
        *that can be sent via ajax, now it is giving error
        */
        console.log(polygonBounds);
        //var myJsonString = JSON.stringify(polygonBounds);
        $.ajax({
            url:"updateProperty.php",
            type: "POST",
            data: {vertices:polygonBounds},
            success:function(e){
                alert(e);
            }
        });
        $(this).after(qry);
        poly.setEditable(false);
        $(this).html("Edit Map");
    }
    return false;
});
Was it helpful?

Solution

replace your comments and 1 line above & below the comments with the following code

 var vertices = poly.getPath();

 var polygonBounds = [];
   for (var i = 0; i < vertices.length; i++) {
   var xy = vertices.getAt(i);
   polygonBounds.push({ lat: xy.lat(), lon: xy.lng() });
  }

 console.log(polygonBounds);

hope this work fine for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top