Leaflet.js: How to remove multiple layers from map

后端 未结 3 1428
囚心锁ツ
囚心锁ツ 2021-02-08 12:18

I am using Leaflet.js for a map. Now I want to remove added layers from the map. By clicking the input #button all checked checkboxes shall be changed to unchecked and all corre

3条回答
  •  醉话见心
    2021-02-08 12:24

    I would say the easiest way to remove/add (toggle) layers from the map would be to use a LayerGroup.

    Before adding individual layers to the map, add them to a LayerGroup instead and then add that LayerGroup to your map.

    Then when you have to remove the layers, just call the clearLayers() function.

    Check out the API for LayerGroup http://leafletjs.com/reference.html#layergroup

    Example

    var map = L.map('leafletMap', {minZoom:11}).setView([37.8, -96], 11);
    var assetLayerGroup = new L.LayerGroup();
    var marker1 = L.marker([39.61, -105.02]).bindPopup('This is Littleton, CO.');
    var marker2 = L.marker([39.74, -104.99]).bindPopup('This is Denver, CO.'),
    assetLayerGroup.addLayer(marker1);
    assetLayerGroup.addLayer(marker2);
    

    When remove checkbox is checked

    assetLayerGroup.clearLayers();
    

提交回复
热议问题