0

I'm trying to pull the attribute data from a GeoJSON layer.

var neighLyr = new L.GeoJSON.AJAX("data/neighbourhood.geojson",{
    }).addTo(mymap);


neighLyr.on('click', function (e) {
    console.log(e.layer.properties.id); 
    });

Why doesn't this code work ? It keeps saying the id is undefined. I'm using https://unpkg.com/leaflet@1.5.1/dist/leaflet.js

  • Your code does not work because e.layer is Leaflet layer object (feature group) which knows nothing about GeoJSON feature properties. If you want to get info about individual GeoJSON features, you have to use onEachFeature option of L.GeoJSON.AJAX object. – TomazicM Jan 16 at 7:57
  • Or go with the event's sourceTarget as per leafletjs.com/reference-1.6.0.html#event-sourcetarget – IvanSanchez Jan 16 at 8:44
  • Nice thanks. I got it to work with the code below. – AdamGIS Jan 16 at 14:22
0

I found out the answer to my question, thanks guys. Below is the code that works.

    var neigh
    var neighLyr = new L.GeoJSON.AJAX("data/neighbourhood.geojson",{
        style: myStyle,
        onEachFeature : function (feature, layer){
            layer.on('mouseover' , function(e) {
                neigh = feature.properties.pri_neigh;
            });
        }
    }).addTo(mymap);

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.