[英]Leaflet-map click event not emitted when layer controls moved out of map-element
我對Leaflet地圖的點擊事件有疑問。
我將圖層控件從地圖元素中移出,如下所示:
document.getElementById('outside-controls').appendChild(ctrl.getContainer());
之后,每次更改圖層控件設置時,我都開始錯過點擊事件。
這是工作示例代碼:
var dom_documentClicks = document.getElementById('document-clicks'),
dom_mapClicks = document.getElementById('map-clicks'),
count_documentClicks = 0,
count_mapClicks = 0;
var osmUrl = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
osmAttrib = '© <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
osm = L.tileLayer(osmUrl, {
maxZoom: 18,
attribution: osmAttrib
}),
poly = L.polygon([]),
ctrl = L.control.layers({}, {'Polygon Layer': poly}, {collapsed: false});
// initialize the map on the "map" div with a given center and zoom
var map = L.map('map', {layers: [osm, poly]}).setView([61.497752, 23.760954], 12);
ctrl.addTo(map);
// This seems to be the reason why click-event is not emitted.
document.getElementById('outside-controls').appendChild(ctrl.getContainer());
function onDocumentClick(e) {
count_documentClicks++;
dom_documentClicks.innerHTML = count_documentClicks;
}
function onMapClick(e) {
count_mapClicks++;
dom_mapClicks.innerHTML = count_mapClicks;
}
map.on('click', onMapClick);
document.addEventListener('click', onDocumentClick);
並鏈接到jsFiddle: http : //jsfiddle.net/LnzN2/135/
有誰知道擺脫這個問題的方法(錯誤?)?
似乎ctrl.getContainer()返回的div消耗了map中的下一個click-event。
更改此內容時:
ctrl.getContainer()
對此:
ctrl.getContainer().childNodes[0]
有用!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.