makinacorpus / Leaflet.FileLayer

Loads files locally (GeoJSON, KML, GPX) as layers using HTML5 File API
http://makinacorpus.github.io/Leaflet.FileLayer/
MIT License
271 stars 91 forks source link

fileLayerLoad() returns undefined property 'loader' #64

Open mwolfhoffman opened 5 years ago

mwolfhoffman commented 5 years ago

I am getting errors that I cannot read the 'on' property of undefined. Therefore, in the example below (copied from the Readme's event example), control.loader is undefined.

    var control = L.Control.fileLayerLoad();
    control.loader.on('data:loaded', function (event) {
        // event.layer gives you access to the layers you just uploaded!

        // Add to map layer switcher
        layerswitcher.addOverlay(event.layer, event.filename);
    });
leplatrem commented 5 years ago

This plugin probably needs to be updated with latest versions of Leaflet...

johnd0e commented 5 years ago

This example is broken indeed. Actually loader property is available only after we add control to map.

So this should work:

 var control = L.Control.fileLayerLoad().addTo(map);
johnd0e commented 5 years ago

BTW, I think that loader could be initialized immediately on control creation (loader._map can be initialized later).

If user tries to use loader before adding control to map it may throw error. But actually I do not see any reason why loader should strictly require map: options.addToMap can be false.

P.S. Also I suppose that L.FileLayer.FileLoader has not to be extended from L.Layer as it's not layer, and does not benefit from any L.Layer-specific properties/methods/events.