简体   繁体   中英

Leaflet-map click event not emitted when layer controls moved out of map-element

I have a problem with Leaflet-map's click-event.

I moved Layers-control out of map-element like this:

document.getElementById('outside-controls').appendChild(ctrl.getContainer());

After that I started to miss click-event every time I change layers-control setup.

Here is working sample code:

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 = '&copy; <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);

And link to jsFiddle: http://jsfiddle.net/LnzN2/135/

Anyone who knows a way to get rid of this issue (bug?)?

It seems that ctrl.getContainer() returns div that consumes next click-event from map.

when changing this:

ctrl.getContainer()

to this:

ctrl.getContainer().childNodes[0]

it works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM