Is it possible to make a click event occur only on the tile layer on a Leaflet map?
I tried
var tiles= L.tileLayer(...).addTo(map);
tiles.on('click', function(e) { console.log('ok') });
but it doesn't do anything. I also tried
tiles.on({ click: console.log('ok') });
but it only triggers once when the map loads.
I'd like this event to occur only on tile layers, not when the user clicks on another layer.
Best Answer
No.
If you read the documentation for
L.TileLayer
, you'll see thatL.TileLayer
s do not have aclick
event.If that's the case, then you should look at attaching a
click
event handler to theL.Map
instance, and useL.DomEvent.stopPropagation
on any otherclick
event handlers (so that e.g. a marker click event won't propagate upwards to the map instance and fire the map'sclick
event handler).