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(){

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

Any suggestions?

  • NB: I used to develop with OpenLayers 2.x for years - now just getting to know Leaflet. The documentation confuses me - I'd expect a super class for all layer types that they would inherit from. The class structure in Leaflet seems less structured to me.
    – jollege
    May 27 '15 at 7:55

You can try:

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


You should bind the event to the map object.


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

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

//and so on
  • Strange, it fires twice, but it doesn't open the popup: var l1 = omnivore.kml('data/file.kml?'); var pop = '<img src="file.jpg"/>'; l1.bindPopup(pop); m.on('layeradd', function(e){ vg.log(e); l1.openPopup() }); l1.addTo(m);
    – jollege
    May 28 '15 at 21:32
  • I have an issue with map 'load' event: m.on('load', function(){ var $ib = $('#vgInfoButtonsContainer'), $lc = $('.leaflet-control-layers:first'); console.log($ib,$lc); $ib.append( $lc.detach() ); }); - the event never fires. If I use e.g. 'focus' or 'click' events, they fire as expected.
    – jollege
    May 29 '15 at 11:22
  • but this is out of the question scope, is your initial question solved? May 29 '15 at 11:59
  • Thanks, it sort of works. I bind KML_layer.openPopup() and a console.log to the map's 'layeradd' event and the popup opens allright. The event fires hundreds or even thousands of times. That is very disturbing!
    – jollege
    May 29 '15 at 13:42
  • Pusling: My workaround before was setTimeout(function(){l1.openPopup()}, 500) which worked fine - i.e. it was not a problem to bind and open a popup on what omnivore returned - only when bound to the 'addlayer' event it was not possible. Combined with the 'load' event never firing - seems to me that events are unreliable in Leaflet?
    – jollege
    May 29 '15 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.