本文介紹了如何使用傳單 map.on('click', function) 事件處理程序將標記添加到地圖的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我正在嘗試使用事件處理程序向地圖添加標記.我可以使用回調函數來管理它,但是當我將函數與事件處理程序分開時就不行了.
I'm trying to use an event handler to add a marker to the map. I can manage this with a callback function, but not when I separate the function from the event handler.
回調(http://fiddle.jshell.net/rhewitt/U6Gaa/7/):
map.on('click', function(e){
var marker = new L.marker(e.latlng).addTo(map);
});
獨立函數(http://jsfiddle.net/rhewitt/U6Gaa/6/):
function newMarker(e){
var marker = new L.marker(e.latlng).addTo(map);
}
推薦答案
在你的小提琴代碼中,你的函數在錯誤的范圍內.嘗試在 map 函數內移動函數,而不是在它自己的范圍內......即,而不是:
in your fiddle code, your function is in the wrong scope. try moving the function inside the map function instead of in it's own scope... i.e. instead of:
});
function addMarker(e){
// Add marker to map at click location; add popup window
var newMarker = new L.marker(e.latlng).addTo(map);
}
使用
function addMarker(e){
// Add marker to map at click location; add popup window
var newMarker = new L.marker(e.latlng).addTo(map);
}
});
這篇關于如何使用傳單 map.on('click', function) 事件處理程序將標記添加到地圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!