Leaflet: Simple click handler not working for Geojson layer on Leaflet 1.0.0-rc.3

Created on 26 Aug 2016  ·  4Comments  ·  Source: Leaflet/Leaflet

I am doing something similar to http://jsfiddle.net/tator/em9cLfk4/4/

This isn't related to layer click bubbled up to map click handler however. It's about the layer click simply doesn't get triggered with code that is working properly for 0.7.7.

I reduced my code to just base tile layer + one overlay layer to make sure there isn't any layer order zindex issues.

The map.on("click") still fires properly but not for the GeoJson layer.

function onEachFeatureHandler(feature, layer){
            layer.on('click', function(e){
                     alert("click");
           });
}

overlay = L.geoJson(GeoJSON, {
       onEachFeature: onEachFeatureHandler
});

I also tried two different ways to do the layer.on("click"), either click as a string, or add a {"click": handlerFuncName}. It doesn't make a difference as expected.

I tried adding the base tile layer before/after the overlay, and nothing changed. I also removed the click handler on map to avoid an event conflict. So I end up with a map with just tile + overlay, and one click event handler, but still, nothing will fire. The mouse cursor indicates that the layer is not clickable at all.

I also modified the colors of overlay to make sure it was properly added to the map object. It is on top of the tilelayer.

This is happening in latest Chrome v52 and Safari 9 on the Mac.

After trying almost 4 hours on this...I suspect it might be something dumb in my code or there are some API changes I miss out on the changelog. I'd appreciate if you can offer some tips on what I might have been doing wrong. Any insight would be appreciated.

Most helpful comment

@mourner After some thinking, I realize I didn't update the css to latest. It may have contributed to the zIndex level not being correct, or disallowed some click event. Now everything seems to be normal just by updating the css to latest. Closing the issue.

All 4 comments

Can you set up an exact minimal test case on JSFiddle or Leaflet Playground reproducing the issue?

@mourner I will try a playground with some simple Geojson and see what happens. I haven't had the click handler code broken in very isolated examples. I even debugged to the layer object in debugger...the click handler is definitely added. A click event is triggered on the tile <img> when I click on the layers.

I can't think of any other factors impacting this.

@mourner just tried it with rc3 on playground, raw geojson layer still works as intended...

@mourner After some thinking, I realize I didn't update the css to latest. It may have contributed to the zIndex level not being correct, or disallowed some click event. Now everything seems to be normal just by updating the css to latest. Closing the issue.

Was this page helpful?
0 / 5 - 0 ratings