0
votes

Using leaflet, I call below function when I add a layer to the map. This function adds popups to each feature.

They work when I click on them but I can't get the bindPopup.openPopup() to work so it opens without being clicked on.

There aren't any errors but the popup doesn't open without being clicked on.

var popupToOpen = null;
var clickedLocationId = 0;
function onEachFeature(feature, layer) {
    if (feature.properties && feature.properties.UserName) {        
        if (feature.properties.MarkerId == clickedLocationId) {            
          layer.bindPopup("<div id='unlockLocationId'>" + feature.properties.MarkerId + "</div><div>" + feature.properties.UserName + "</div>").openPopup();
        } else {
            layer.bindPopup("<div id='unlockLocationId'>" + feature.properties.MarkerId + "</div><div>" + feature.properties.UserName + "</div>");
        }
    }
}
1

1 Answers

0
votes

I've tried the way you're trying but the popup doesn't open. Well, you can try opening popup another way.

Create a function where you iterate your geojson layer and open the popup if the id matches. Here is the function

function openMarkerPopup(id){
    geojson.eachLayer(function(feature){
        if(feature.feature.properties.id==id){
            feature.openPopup();
        }

    });
}

Simply pass the required id to this function, and it'd work.

openMarkerPopup(108);

Here is a working fiddle