2
votes

I am trying to bind popups to markers on a geoJSON layer. To do this, I am using the onEachFeature function:

var onEachFeature = function(feature, layer) {
                    layer.bindPopup("hello",
                                   {closeButton:false,
                                    autoClose: false,
                                    closeOnClick: false,
                                    className: "popup-custom"}).openPopup();
}

This does not result in a popup showing. The popup is created but I need to click on the marker to display it. What am I missing to make the popup visible without clicking on the marker?

I am not using the pointToLayer function because I am also filtering the features with the filter fuction and using request to customise the popups and the markers (the popup binding is actually in a callback function).

2

2 Answers

1
votes

You get a geojsonlayer as result and then you can open the popup for each layer:

var geojsonLayer = L.geoJSON(data, {
    onEachFeature : onEachFeature 
}).addTo(map);

geojsonLayer.eachLayer(function(layer){
    layer.openPopup();
});
0
votes

Thank you @Falke-Design, I got it to work. I had tried this but it did not work :

var geojsonLayer = L.geoJSON(data, {
    onEachFeature : onEachFeature 
});

geojsonLayer.eachLayer(function(layer){
    layer.openPopup();
});

geojsonLayer.addTo(map);

Could you explain why this did not work?