0

我正在尝试在我正在创建的 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 },
   });
4

2 回答 2

1

我放弃了leaflet draw library和snap,改用leaflet-geoman library。捕捉工作完美。

于 2020-01-14T17:31:03.593 回答
1

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

对于上述包中不存在的更多功能,您可以将其用于折线 https://www.npmjs.com/package/leaflet-linestring-editor

于 2020-01-21T12:54:53.750 回答