【问题标题】:leaflet.draw trash button delete all polygons and saveLeaflet.draw 垃圾按钮删除所有多边形并保存
【发布时间】:2014-01-14 22:40:05
【问题描述】:

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

【问题讨论】:

标签: javascript leaflet leaflet.draw


【解决方案1】:

用自定义控件解决了我自己的问题(感谢 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);

【讨论】:

    【解决方案2】:

    像 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);
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 这需要在使用map.addControl(drawControl)之前完成;
      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 1970-01-01
      • 2015-03-14
      • 2015-12-16
      • 1970-01-01
      相关资源
      最近更新 更多