[GIS] Does Leaflet layer ‘add’ event not fire properly

javascriptleaflet

A popup won't open until the data file has loaded and the layer is added to the map – that's pretty obvious. I figured it would help to bind it to the 'add' event – but that never fires:

var l1 = omnivore.kml('data/Niveauregulering01a.kml');
var pop = '<img src="content/img/whatever.jpg" alt="..." style="width:300px;"/>'
l1.on('add', function(){
  console.log('add');
  l1.openPopup();
});
l1.bindPopup(pop).addTo(map);

There seems to be no other event that I can bind it to:
http://leafletjs.com/reference.html#path-add

Any suggestions?

Best Answer

You can try:

map.on('layeradd', function(layer, layername){

});

You should bind the event to the map object.

/**UPDATE****/

You cannot bind the popup directly to omnivore.kml, according to their docs the returned object is a L.geoJson() layer -- so a pool of features.

Try to bind the popup on each feature:

var KML_layer = L.geoJson(null, {
    onEachFeature: function (feature, layer) {
        layer.bindPopup(feature.properties.description);
    }
}).addTo(map);

var runLayer = omnivore.kml('line.kml', null, KML_layer);

//and so on