带有 geojson 多边形层的 Leaflet Draw 和 Leaflet Snap

Leaflet Draw and Leaflet Snap with geojson polygon layers

我正在尝试在我正在创建的 Leaflet 应用程序中启用捕捉(使用 Leaflet Snap)。我正在使用传单抽奖。现有图层从 geojson 中的数据库中读取。我将这些添加到 guideLayers 并在那里添加新创建的功能。它不工作。有没有人能够成功地创造出这样的东西?也就是说,能够创建一个新的多边形并捕捉到传单(geojson 层)中的现有多边形吗?谢谢丹。

Add geojson to guideLayers code:

function renderLta(_ltas,ltaLayerName) {

L.geoJSON([_ltas.geoJson], {
        name:_ltas.id,
        areaName:_ltas.localThreatAreaName,
        areaDescription:_ltas.localThreatAreaDescription,
        style: function (feature) {
        return _getLtaStyle(1);         
        },      
        onEachFeature: function onEachFeature(feature, layer) {
            ltaLayerName.addLayer(layer);
            guideLayers.push(layer);
            layer.on('click', function(e) {
                if(selectedFeature) {
                    selectedFeature.editing.disable();
                    // Has there been a change? Does the user need to save?
                    // get layer again and redraw
                       drawnItems.removeLayer(selectedFeature);
                       selectedFeature.addTo(map_lta);
                }
                selectedFeature = e.target;
                e.target.editing.enable();
                drawnItems.addLayer(e.target);                              
            });             
        }       
       });

        ltaLayerName.addTo(map);

Add new layer/data to guideLayers code:

        map.on(L.Draw.Event.CREATED, function(event) {
        var layer = event.layer;
        var content = getPopupContent(layer);
        if (content !== null) {
            layer.bindPopup(content);
        }
        drawnItems.addLayer(layer);
        guideLayers.push(layer);
        });

DrawControl Code:

var drawControl = new L.Control.Draw({
        edit: {
            featureGroup: drawnItems,
            poly : {
                allowIntersection : false
            }
        },
        draw: {
            polyline: false,
            polygon : { showArea: true, allowIntersection : false, guideLayers: guideLayers, snapDistance: 500 },
            circle: false,
            rectangle: false,
            marker: false,
            circlemarker: false
        }
    });

    map_lta.addControl(drawControl);

    drawControl.setDrawingOptions({
        polygon: { guideLayers: guideLayers, snapDistance: 50 },
   });

我放弃了 leaflet draw 库和 snap,转而使用 leaflet-geoman 库。捕捉工作完美。

您可以使用以下库来绘制和捕捉 https://www.npmjs.com/package/@geoman-io/leaflet-geoman-free

对于上述包中没有的更多功能,您可以使用这个包,尤其是折线 https://www.npmjs.com/package/leaflet-linestring-editor