首页 文章

Leaflet绘制 Map 并导入保存的数据错误

提问于
浏览
0

我已经创建了一个mapbox-gl和leaflet map,我正在尝试使用插件leaflet-draw添加和编辑一个图层 .

该插件运行良好;我可以创建一个路径(折线)并使用geojson保存它 .

我重新加载包含已保存数据的 Map 时出错:

TypeError: dataLayer.push is not a function
TypeError: a.slice is not a function

plyline在 Map 上,但如果我尝试编辑它(点击“编辑图层”按钮),折线会改变颜色(这意味着它被选中)但没有显示锚点,也无法拖动或编辑 .

dataLayer是我创建的图层,包括新数据 .

JSON数据在http://geojson.io/#map=19/44.93382/9.67409上验证,看起来不错 .

我想我没有正确创建dataLayer .

// ADD data stored in DB
  var geojson = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            9.67420756816864,
            44.93391475147485
          ],
          [
            9.674186110496521,
            44.93359955072641
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          9.67420756816864,
          44.933929941828694
        ]
      }
    }
  ]
};

    var dataLayer = L.geoJson(geojson);

    // featureGroup is the layer editable
    var featureGroup = L.featureGroup();

    // add  data from DB  to editable layer
    dataLayer.addTo(featureGroup);

    // add editable layer to the Map
    featureGroup.addTo(map);

我的代码有什么问题?

1 回答

  • 0

    Geojson标准不包含额外的功能,所以我只有路径,而不是额外的项目,如锚点(矩形) . 要解决它,我需要将代码修复为:

    // featureGroup is the layer editable
        var featureGroup = L.featureGroup();
        featureGroup.addTo(map);
    
    
        L.geoJson(geojson, {
            onEachFeature: function (feature, layer) {
                if (layer.getLayers) {
                    layer.getLayers().forEach(function (l) {
                        featureGroup.addLayer(l);
                    })
                } else {
                    featureGroup.addLayer(layer);
                }
            }
        });
    

相关问题