ebrelsford / Leaflet.loading

A simple loading control for Leaflet
MIT License
143 stars 49 forks source link

Show the loader when the map is initialised. #28

Open arfa opened 8 years ago

arfa commented 8 years ago

Hi, I think that the loader must be displayed by default. Later when the layer is loaded, the event load will trigger and the event handler will hide the loader.

In fact the event loading is triggred before the method _addLayerListeners is called. so the plugin dosn't handle the first time the map is loading.

I suggest the following fix.

onAdd: function(map) {
    ...

    // Create the loading indicator
    var classes = 'leaflet-control-loading';

    ...
}

becomes

onAdd: function(map) {
    ...

    // Create the loading indicator
    var classes = 'leaflet-control-loading is-loading';

    ...
}

This way the loader appears when the map is loading for the first time.

ebrelsford commented 8 years ago

Yes, this has been on my mind, the only issue is that this solution assumes that a load event will be triggered at some point. That's impossible to ensure if we didn't detect loading initially.

I'd be interested to see if there's a way to add the event listeners early enough to ensure that we get all the loading events.

benjeffery commented 8 years ago

I got around this by checking if the _loading flag is set. Prob a bit hacky but fixed the issue for me. See https://github.com/benjeffery/Leaflet.loading/commit/339c1338753a9f5c7d8a074628da19093906eae3

ebrelsford commented 8 years ago

If someone wanted to add this as an option and submit a PR I'd fold it in. I don't think we should rely on an internal member (_loading) by default.