I have a GeoJSON layer that comes from a fetch function in another js module. I call this layer through an await function and therefore, I cannot use L.geojson(data, {onEachFeature: onEachfeature}). I need to add the onEachFeature
function after loading the data.
I tried to add it through an empty GeoJSON but it doesn't work since I'm calling my layer's attributes in the onEachFeature
function.
Does anyone know the solution?
Code :
// Load the layer
const communes_lyr = await getLayer4('communes');
// Set the style (ideally, a function like this but for onEachFeature would be great!)
communes_lyr.setStyle(com_style)
// Create an empty geojson to add the oneachfeature function
var empty_com = L.geoJSON(false, {
onEachFeature: function (feature, communes_lyr) {
communes_lyr.bindPopup(feature.properties.COMMUNE + "<br>" + "<br>"
+ "Pollution lumineuse = " + feature.properties.intensity + "%" + "<br>"
+ "Population = " + feature.properties.POPULATION + " habitants" + "<br>"
+ "Superficie = " + feature.properties.superficie + "km²")
}
}).addTo(map);
// Add the empty layer with the function to my layer
communes_lyr.addData(empty_com);
L.GeoJSON
do have aeachFeature()
method, inherited fromL.LayerGroup
. It's in the API documentation.