0

as title says I have problems removing maps before adding a new one, I have this function, which draws the tile given by a Spinner (parameter values are strings with correct leaflet tile names):

function drawMap(tile){ 
    map.addLayer(tile);
    map.eachLayer(function (layer) {
        if (layer !== tile) {
            map.removeLayer(layer);
        }
   });
}

Variable map is initialized this way, and it doesn't generate problems:

var map = L.map('map').fitBounds([
        [myPosJSON.NEBoundLat, myPosJSON.NEBoundLng],
        [myPosJSON.SWBoundLat, myPosJSON.SWBoundLng]
    ]);

How can I fix the drawMap() function to correctly display maps? (I also have to do it with a group of Overlays, but probably the procedure is the same for maps)

Enrico Casanova
  • 51
  • 2
  • 10

1 Answers1

1

Instead of checking for name after adding your new layer, try removing all layers first and then add the new layer:

function drawMap(tile){ 
    map.eachLayer(function (layer) {
        map.removeLayer(layer);
    });
    map.addLayer(tile);
}
Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41