7

使用javascript,如何更改leaflet.draw“垃圾箱”按钮以删除所有已绘制并自动保存的多边形。下面是我实现的代码,但它是一个完整的 hack。它会删除活动的多边形,但是在我删除一个对象后,当我单击“垃圾箱”图标时开始在控制台中出现错误,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();
            }
        });
4

3 回答 3

8

使用自定义控件解决了我自己的问题(感谢 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);
于 2014-01-31T02:39:53.693 回答
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);
于 2014-08-26T12:49:51.977 回答
4

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

L.EditToolbar.Delete.include({
  enable: function () {
    this.options.featureGroup.clearLayers()
  }
})
于 2019-09-05T17:17:20.903 回答