Leaflet.js: How to remove multiple layers from map

后端 未结 3 1430
囚心锁ツ
囚心锁ツ 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:21

    According to the Leaflet API doc http://leafletjs.com/reference.html#map-removelayer, removeLayer takes an ILayer parameter: removeLayer( layer ) but you're passing it a String: $(this).attr("id")

    It looks like you do have the layer object in a variable already: mapcat52. You could save the layer objects when you create them, then look them up by id to pass to removeLayer:

    var layers = new Array();
    
    // create layer
    var mapcat52 = new MyCustomLayer(latlng);
    
    // save to layers list
    layers["mapcat52"] = mapcat52;
    ...
    
    // remove layers
    $.each(someObj.idsChecked, function(id, val) {
        // look up layer object by id
        var layerObj = layers[val];
        // remove layer
        map.removeLayer(layerObj); 
    });
    

提交回复
热议问题