Leafletjs detect click/touch on layer

the layer is available from the event; in your case e.layer returns the layer

alternatively, you save the layer to a variable when you construct it with L.geoJson, then bind your click event later:

var streets = L.geoJson(data.streets, {
    onEachFeature: function(feature, featureLayer) {
        featureLayer.bindPopup(feature.properties.name);
    })
)}
streets.on('click', function(e) { console.log(e.layer) });

This might be better; as it is now, I think you are binding a click event to the layer for every feature, which is probably not what you want to do.


I was assigning layers to a featureGroup, I needed to put the click handler on the featureGroup in order to get a layer reference returned. Putting it on the individual layer didn't work.

Tags:

Leaflet