【问题标题】:The markers drawn on leaflet.draw are not deleted after removing layer在leaflet.draw 上绘制的标记在移除图层后不会被删除
【发布时间】:2020-10-06 08:42:48
【问题描述】:

我创建了一个简单的应用程序来使用传单绘制添加点。如果用户取消对话框,则删除绘制的图层。但是,当绘制下一个标记时,先前已取消的标记再次出现。这是代码的一部分:

// Create Leaflet Draw Control for the draw tools and toolbox
var drawControl = new L.Control.Draw({
  draw : {
    polygon : false,
    polyline : false,
    rectangle : false,
    circle : false,
    circlemarker: false
  },
  edit : false,
  remove: false
});

// Boolean global variable used to control visiblity
var controlOnMap = false;

// Create variable for Leaflet.draw features
var drawnItems = new L.FeatureGroup();

// Function to add the draw control to the map to start editin
function startEdits(){
  if(controlOnMap == true){
    map.removeControl(drawControl);
    controlOnMap = false;
  }
  map.addControl(drawControl);
  controlOnMap = true;
};

// Function to remove the draw control from the map
function stopEdits(){
  if(controlOnMap == true){
    map.removeControl(drawControl);
    controlOnMap = false;
  }else{
    alert("Edition mode is already stopped")
  };
};

// Use the jQuery UI dialog to create a dialog and set options
var dialog = $("#dialog").dialog({
  autoOpen: false,
  height: 600,
  width: 500,
  modal: true,
  position: {
    my: "center center",
    at: "center center",
    of: "#map"
  },
  buttons: {
    "Add": setData,
    "Cancel": function() {
      dialog.dialog("close");
      map.removeLayer(drawnItems);
    }
  },
  close: function() {
    dialog.dialog("close");
    map.removeLayer(drawnItems);
  }
});

是否有移除绘制的特征或类似的功能?

【问题讨论】:

    标签: leaflet leaflet.draw


    【解决方案1】:

    您还必须清除drawnItems 组:

    drawnItems.clearLayers();
    map.removeLayer(drawnItems);
    

    较新的绘图库是:Geoman.io

    【讨论】:

      【解决方案2】:

      刚才我找到了这个解决方案:

          drawnItems.eachLayer(
              function(l){
                  drawnItems.removeLayer(l);
          });
      

      但 Falke 设计的答案更加奇特。谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-17
        • 1970-01-01
        相关资源
        最近更新 更多