我有一个启用了绘图管理器的谷歌地图,用户可以在上面绘制多边形并将其保存到我的数据库中。我为drawingManager对象添加了一个overlaycomplete事件的监听器。当事件被触发时,多边形的坐标将被写入隐藏字段。这个方案很好——唯一的问题是,如果在此之后更改/拖动单个顶点,则不会触发事件。我需要在任何更改时更新该字段或在用户点击提交时迭代多边形顶点并将它们写入隐藏字段。我无法完全弄清楚如何使其工作,但您可以在这里查看我目前的进展: http://jsfiddle.net/5Y4WT/21/。
HTML:
<div id="map_canvas" style="width:500px; height:450px;"></div>
<form method="post" accept-charset="utf-8" id="map_form">
<input type="text" name="vertices" value="" id="vertices" />
<input type="button" name="save" value="Save!" id="save" />
</form>
JavaScript:
var map; // Global declaration of the map
var iw = new google.maps.InfoWindow(); // Global declaration of the infowindow
var lat_longs = new Array();
var markers = new Array();
var drawingManager;
function initialize() {
var myLatlng = new google.maps.LatLng(40.9403762, -74.1318096);
var myOptions = {
zoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [google.maps.drawing.OverlayType.POLYGON]
},
polygonOptions: {
editable: true
}
});
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, "overlaycomplete", function(event) {
var newShape = event.overlay;
newShape.type = event.type;
});
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
$('#vertices').val(event.overlay.getPath().getArray());
});
}
initialize();
$(function(){
$('#save').click(function(){
//iterate polygon vertices?
});
});