我使用谷歌地图api来绘制多边形 现在我想在数据库中保存多边形的顶点
我的多边形代码非常冗长,因为它是从dabase和编辑/删除模式等获取的
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
});
我创建了一个编辑地图和设置地图的按钮 在set map上我想通过ajax
发送getpath()来保存DB中的顶点$("#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;
});
答案 0 :(得分:1)
替换您的评论和上面的1行&使用以下代码在评论下方
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);
希望这对你有用。