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

Existing polygon not remove if load a new one #63

Open ghost opened 5 years ago

ghost commented 5 years ago

I have seen a problem that if i load a KML file and display polygon on map using this plugin, and when drag and drop a new KML file or load from button, previous polygon still remain on map.

johnd0e commented 5 years ago

It is definitely not bug. We can load multiple layers, it's a feature.

To keep loaded single layer only we should remove previous, e.g.:

  var lastLayer;
  control.loader.on('data:loaded', function (e) {
    if (lastLayer) { lastLayer.remove(); }
    lastLayer = e.layer;
  });

Alternatively, you may prefer different approach, involving layers control: https://github.com/makinacorpus/Leaflet.FileLayer/issues/61#issuecomment-476535194

Also both approaches could be combined, e.g.:

  var layerswitcher = L.control.layers().addTo(map);
  var lastLayer;
  control.loader.on('data:loaded', function (e) {
    if (lastLayer) { layerswitcher.removeLayer(lastLayer); }
    layerswitcher.addOverlay(e.layer, e.filename);
    lastLayer = e.layer;
  });