一次绘制、编辑和保存多个多边形 Leaflet.Editable

Draw, edit and save several polygons at once Leaflet.Editable

在 Leaflet.Editable 我需要绘制几个多边形,能够编辑它们并在最后将它们保存为 GeoJSON 或通过外部按钮将它们一起删除。我可以用一个多边形来完成:

map = new L.Map('mapa', {
    maxBounds: extent,
    minZoom: 12,
    zoomControl:false,
    touchZoom: true,
    tap: false,
    editable:true,
    attributionControl:true,
    zoomControl: true
    });
    
$('body').on('click','#start_drawing',function(e){
    newPolygon=map.editTools.startPolygon('',{color:'#d7191c', opacity:0.8, weight: 5,fillColor:'#d7191c', fillOpacity:0.2});
});

$('body').on('click','#delete_drawing',function(e){
    map.removeLayer(newPolygon);
});

$('body').on('click','#save_drawing',function(e){
    newPolygon.toGeoJSON();
});

我尝试为 LeafletEditable 设置 featureLayer,但它创建了空的 LayerGroup。

感谢您的帮助, 旦

将新多边形分组是可行的方法,但您需要设置一个组以将它们保存在控件的回调之外。首先向地图添加一个空组,然后在创建新多边形时向其添加新多边形。按照这些思路应该可以工作:

map = new L.Map('mapa', {
    maxBounds: extent,
    minZoom: 12,
    zoomControl:false,
    touchZoom: true,
    tap: false,
    editable:true,
    attributionControl:true,
    zoomControl: true
});

const fg = L.featureGroup().addTo(map);

$('body').on('click','#start_drawing',function(e){
    const newPolygon=map.editTools.startPolygon('',{color:'#d7191c', opacity:0.8, weight: 5, fillColor:'#d7191c', fillOpacity:0.2});
    fg.addLayer(newPolygon);
});

$('body').on('click','#delete_drawing',function(e){
    fg.clearLayers();
});

$('body').on('click','#save_drawing',function(e){
    let geoJSON = fg.toGeoJSON();
    // Do something with geoJSON...
});