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

jdu*_*hls 6 javascript leaflet leaflet.draw

使用javascript,如何更改leaflet.draw"废纸篓"按钮以删除已绘制并自动保存的所有多边形.下面是我实现的代码,但它是一个完整的黑客.它删除了活动多边形,但是当我在控制台中出现错误时删除一个对象后,当我单击"垃圾桶"图标时NotFoundError: Node was not found,TypeError: this._deletedLayers is null

map.on('draw:editstart', function (e) {
            if(e.handler == 'remove' && typeof drawnItem != 'undefined' && drawnItem !== null){
                if(window.console) window.console.log('Drawing deleted...');
                if(typeof drawnItem != 'undefined' && drawnItem !== null){
                    drawnItems.removeLayer(drawnItem);
                }
                $('.leaflet-draw.leaflet-control .leaflet-draw-actions').hide();
                $('.leaflet-popup-pane .leaflet-draw-tooltip').remove();
            }
        });
Run Code Online (Sandbox Code Playgroud)

jdu*_*hls 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);
Run Code Online (Sandbox Code Playgroud)


小智 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);
Run Code Online (Sandbox Code Playgroud)


luc*_*cas 5

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

L.EditToolbar.Delete.include({
  enable: function () {
    this.options.featureGroup.clearLayers()
  }
})
Run Code Online (Sandbox Code Playgroud)