【问题标题】:How to edit loaded geoJson with Leaflet如何使用 Leaflet 编辑加载的 geoJson
【发布时间】:2016-05-21 12:07:03
【问题描述】:

我想从数据库中检索多边形数据,然后对其进行编辑。我可以检索多边形(存储为 geojson),但不能使它们可编辑。我该怎么做?

    var drawnItems = new L.FeatureGroup();
    map.addLayer(drawnItems);

    var drawControl = new L.Control.Draw({
        edit: {
            featureGroup: drawnItems
        }
    });
    map.addControl(drawControl);

    map.on('draw:created',function(e) {
    e.layer.addTo(drawnItems);
    });

    L.control.layers(baseLayers).addTo(map);    

    var oldPolygon = null;
    function showOnMap(rowid){
    if(oldPolygon != null){
    map.removeLayer(oldPolygon);
    }

    $.get("testdbextract.php?show="+rowid,function(data){
        var newPolygon = L.geoJson(data);
        newPolygon.addTo(drawnItems); // or drawnItems.addLayer(newPolygon);
        oldPolygon = newPolygon;
        }); 
    }

【问题讨论】:

    标签: javascript leaflet leaflet.draw


    【解决方案1】:

    在您的示例中,您需要解析收到的 geojson 数据,创建图层并初始化 drawnItems

    为方便起见,您可以像这样创建GeoJson 层:

    // Create a GeoJson layer without adding it to the map
    L.geoJson(geojsonFeatures, {
      onEachFeature: onEachFeature
    });
    
    // Take advantage of the onEachFeature callback to initialize drawnItems
    function onEachFeature(feature, layer) {
      drawnItems.addLayer(layer);
    }
    

    这是example

    在你的代码中,可以这样使用

    $.get("testdbextract.php?show="+rowid,function(data){
       L.geoJson(data, {
          onEachFeature: onEachFeature
       });
    }); 
    

    【讨论】:

      猜你喜欢
      • 2020-01-04
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多