leaflet.draw垃圾按钮删除所有多边形并保存

时间:2014-01-14 22:40:05

标签: javascript leaflet leaflet.draw

使用javascript,如何更改leaflet.draw“废纸篓”按钮以删除已绘制并自动保存的所有多边形。下面是我实现的代码,但它是一个完整的黑客。它会删除活动多边形,但是当我单击“{1}}和NotFoundError: Node was not found

之类的”废纸篓“图标后,当我开始在控制台中出现错误时删除对象后
TypeError: this._deletedLayers is null

3 个答案:

答案 0 :(得分:7)

使用自定义控件解决了我自己的问题(感谢stackexchange - https://gis.stackexchange.com/questions/60576/custom-leaflet-controls):

已更新!添加了@SpiderWan建议(谢谢!)所以不需要自定义CSS。此外,事件先前已附加到整个控制栏。而只是附加到controlUI按钮本身。

使用Javascript:

L.Control.RemoveAll = L.Control.extend({
        options: {
            position: 'topleft',
        },

        onAdd: function (map) {
            var controlDiv = L.DomUtil.create('div', 'leaflet-control leaflet-bar');
            var controlUI = L.DomUtil.create('a', 'leaflet-draw-edit-remove', controlDiv);
            controlUI.title = 'Remove all drawn items';
            controlUI.setAttribute('href', '#');

            L.DomEvent
                .addListener(controlUI, 'click', L.DomEvent.stopPropagation)
                .addListener(controlUI, 'click', L.DomEvent.preventDefault)
                .addListener(controlUI, 'click', function () {
                    drawnItems.clearLayers();
                    if(window.console) window.console.log('Drawings deleted...');
                });
            return controlDiv;
        }
    });

removeAllControl = new L.Control.RemoveAll();
map.addControl(removeAllControl);

答案 1 :(得分:6)

与jduhls的答案相似,但使用Leaflet.draw CSS类:

L.Control.RemoveAll = L.Control.extend(
{
    options:
    {
        position: 'topleft',
    },
    onAdd: function (map) {
        var controlDiv = L.DomUtil.create('div', 'leaflet-draw-toolbar leaflet-bar');
        L.DomEvent
            .addListener(controlDiv, 'click', L.DomEvent.stopPropagation)
            .addListener(controlDiv, 'click', L.DomEvent.preventDefault)
        .addListener(controlDiv, 'click', function () {
            drawnItems.clearLayers();
        });

        var controlUI = L.DomUtil.create('a', 'leaflet-draw-edit-remove', controlDiv);
        controlUI.title = 'Remove All Polygons';
        controlUI.href = '#';
        return controlDiv;
    }
});
var removeAllControl = new L.Control.RemoveAll();
map.addControl(removeAllControl);

答案 2 :(得分:0)

您还可以覆盖删除工具的enable方法,以简单地删除所有图层,而不用打开删除菜单:

L.EditToolbar.Delete.include({
  enable: function () {
    this.options.featureGroup.clearLayers()
  }
})