我有一个启用了绘图管理器的谷歌地图,用户可以在其中绘制一个多边形并将其保存到我的数据库中。我为overlaycomplete 事件向drawingManager 对象添加了一个事件侦听器。触发事件时,多边形的坐标将写入隐藏字段。这很好用——唯一的问题是,如果在该点之后拖动/更改各个顶点,则不会触发事件。当用户点击提交并将它们写入隐藏字段时,我需要更新(任何)更改字段或遍历多边形顶点。我不太清楚如何让它工作,但你可以在这里看到我到目前为止所拥有的: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?
});
});