I am trying to draw country shapes on a leaflet map using L.GeoJSON(data).addTo(map)
. I then want to bind a popup to the click event of that country shape...
new L.GeoJSON(data, {
onEachFeature: function(feature, layer) {
layer['on']('click', popupFunction);
}
}).addTo(this.map);
popupFunction = function(event) {
var layer = event.target;
// Open the 'add' popup and get our content node
var bound = layer.bindPopup(
"<div>Hello World!</div>"
).openPopup();
// Ugly hack to get the HTML content node for the popup
// because I need to do things with it
var contentNode = $(bound['_popup']['_contentNode']);
}
Now this works fine when the data is a single polygon, because then the layer
attribute passed to the onEachFeature function is just that: a layer.
However if the data
is a multipolygon (i.e. the US) this stops working because the "layer
" is now a layerGroup (it has a _layers
) attribute and therefore has no _popup
attribute and so I can't get the _contentNode
for the popup.
It seems like this should be quite a common thing, wanting a popup on a layerGroup. Why does it have no _popup
attribute?