user4131013
user4131013

Reputation: 445

How to open leaflet marker popup from data geojson with href

I get markers and data popups from geojson.

I want to open a specific popup from href. I need you to open popup using its ID or another way.

I saw this example but I don't know how can I implement it in my code.

Here is my sample geojson data

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-67.9283981,10.1497326]},"properties":{"id":107,"text":"Marker 1"}}

and here is my code

$.getJSON('get_mapa_getjon.php', function(data) {
    var geojson = L.geoJson(data, {
        onEachFeature: function (feature, layer) {
            layer.bindPopup(feature.properties.id + '<br />' + feature.properties.text);
            }
    });
geojson.addTo(map);

Upvotes: 1

Views: 3656

Answers (1)

muzaffar
muzaffar

Reputation: 1736

You need to loop over the geojson layer and check for feature property like id in our case this way

geojson.eachLayer(function(feature){ //geojson is the object which have your data

    if(feature.feature.properties.id=='required-id'){ //insert the id in place of 'required-id'
        feature.openPopup(); //open popup for matching ID
    }
    //remove the below line if you have multiple features with same ID
    break;//exit loop once it opens the popup
});

Here is a working fiddle

Upvotes: 1

Related Questions