简体   繁体   中英

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM