Manuel
Manuel

Reputation: 2542

Leaflet map-event 'load' does not fire

I am trying to call a function after a leaflet map has successfully loaded, working with leaflet 1.0.1. Regarding to the docs there is a map event named load which states: Fired when the map is initialized (when its center and zoom are set for the first time). So this snipped should fire, if the map has loaded, but that never happens:

function onMapLoad() {
    alert("Map successfully loaded")
};

mymap.on('load', onMapLoad);

There is no error or response, simple nothing. So why is the map-load event not working properly ?

Here goes a simple JS FIDDLE.

Upvotes: 20

Views: 30608

Answers (3)

Gaetan LOISEL-MONTACQ
Gaetan LOISEL-MONTACQ

Reputation: 383

I suggest you to use the "whenReady" method available in Leaflet 1.0.1

var callBack = function () {
    console.log("Map successfully loaded");
    // do some stuff
};

mymap.whenReady(callBack);

Upvotes: 27

Mahdi Bashirpour
Mahdi Bashirpour

Reputation: 18803

Or use inline

var mymap = L.map('YOUR_MAP_ID').on('load', onMapLoad).setView([51.505, -0.09], 13);

function onMapLoad(){
  console.log("map loaded");
}

Upvotes: 1

Gaurav Gandhi
Gaurav Gandhi

Reputation: 3201

Put the mymap.on('load', onMapLoad); event handler before you actually load the map (with map.setView...).

So your actual code should look like,

var mymap = L.map('mapid');
mymap.on('load', onMapLoad);
mymap.setView([51.505, -0.09], 13);

Source : Github Issue

Upvotes: 28

Related Questions