我試圖在谷歌地圖上創建一個可編輯的多邊形,在更改時返回多邊形的地理座標。在下面的JS Fiddle中,你會注意到如果你創建了一個三角形,然後拖動其中的一個點,那麼事件監聽器就不會選擇點擊;但是,當然,如果再次點擊該點,它會識別它。谷歌地圖API如何讓點擊偵聽器識別拖動
有沒有什麼辦法可以將事件監聽器添加到谷歌地圖中以便「拖動」多邊形事件?
http://jsfiddle.net/mclean25/pnc4ttb0/5/
HTML
<div id="map_canvas" style="width:500px; height:450px;"></div>
<textarea height="100" wid name="vertices" value="" id="vertices" ></textarea>
的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){
overlayClickListener(event.overlay);
var values = event.overlay.getPath().getArray();
for (var i = 0; i < values.length; i++){
//console.log("lat:", values[i].lat());
//console.log("lng:", values[i].lng());
}
$('#vertices').val(event.overlay.getPath().getArray());
});
}
function overlayClickListener(overlay) {
google.maps.event.addDomListener(overlay, "click", function(event){
console.log("Changing this guy!");
$('#vertices').val(overlay.getPath().getArray());
});
}
initialize();
$(function(){
$('#save').click(function(){
//iterate polygon vertices?
});
});
你想要什麼,不要喲你想拖動多邊形?或點擊其標記?或拖累其製造商? –
是的,拖動多邊形並讓聽者識別這個改變。從那裏我將記錄每個點的新緯度/經度。 – thefoxrocks