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

时间:2019-10-23 13:26:34

标签: leaflet geojson snapping leaflet-draw

我正在尝试在正在创建的Leaflet应用程序中启用捕捉(使用Leaflet Snap)。我正在使用Leaflet Draw。现有层是从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 },
   });

2 个答案:

答案 0 :(得分:1)

我放弃了传单绘制库和快照,而是使用了传单-geoman库。捕捉效果完美。

答案 1 :(得分:1)

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

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