Leaflet Map – Trigger Event on Tile Layer in Leaflet

leaflet

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

Is it possible to make a click event occur only on the tile layer on a Leaflet map?

No.

If you read the documentation for L.TileLayer, you'll see that L.TileLayers do not have a click event.

I'd like this event to occur only on tile layers, not when the user clicks on another layer.

If that's the case, then you should look at attaching a click event handler to the L.Map instance, and use L.DomEvent.stopPropagation on any other click event handlers (so that e.g. a marker click event won't propagate upwards to the map instance and fire the map's click event handler).