3

Leaflet has a 'click' event:

map.on('click', function(e) {
  alert(e.latlng);
});

This does not appear to work when map layers are displayed on the area clicked. Is there a way to use map.on('click') so that it ignores any layers on the map?

2 Answers 2

1

When you have a layer that also listens for click then it will pick up the top most layer's event.

Check out this fiddle.

You can add the shape click handler and see that it picks that up instead of the map's.

3
  • Hi, thanks for the reply. This is the problem I am trying to solve- I want to avoid having to manually set up the click handling for each layer (my map has 8+ layers). Thanks again.
    – Matt
    Commented Jul 23, 2015 at 3:18
  • @Matt: you could create custom events on the layers (layer.on("myEvent",function(e){...});) and trigger them in the click-event callback of the map (layer.fire("myEvent");)
    – christoph
    Commented Jul 23, 2015 at 5:37
  • Layers inherit from Evented - so follow e.g. github.com/Leaflet/Leaflet/blob/master/src/core/Events.js
    – christoph
    Commented Jul 23, 2015 at 5:39
1

If you do (or want to) use the Leaflet 1.0 Beta version, Marker and Path have an interactive option.

If false, the marker will not emit mouse events and will act as a part of the underlying map.

(from leaflet beta API)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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