0

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.

2

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.