Reputation: 305
I have a geojson layer in leaflet and I would like to trigger a popup on a specific feature. When declaring the geoJson layer I already have an onEachFeature property which will trigger the popup on click. This works great.
onEachFeature: function (feature, layer) {
layer.bindPopup('<div><h1>' + feature.properties.name + '</h1></div><div>' + feature.properties.description + '</div>');
}
I would like to trigger this popup programmatically though, say on an event. How would I go about that?
Thanks!
Upvotes: 2
Views: 700
Reputation: 12882
You have to select the needed layer and call .openPoup()
method. For example:
var geoJson = L.geoJson(geoJsonData, {
onEachFeature: onEachFeature
}).addTo(map);
geoJson.getLayer(layerId).openPopup()
I've made a fiddle for you: http://jsfiddle.net/wz3Lj7v4/15/ . The main issue is how you get the layer that you need. You can look towards .getLayer()
, .getLayers()
and .eachLayer()
methods.
Upvotes: 3