LeafletJS GeoMan 转换为 JSON 数据

LeafletJS GeoMan into JSON data

我已经将 GeoMan 添加到我的传单地图中,只是想知道是否有一种方法可以将所有绘制的要素导出到 JSON。我仅将其用于开发,因此 JSON 可以进入 console.log

我正在努力解决这个问题。这是我迄今为止唯一的代码

map.pm.toggleGlobalDragMode();
map.pm.addControls({
  position: 'topright',
  editMode: true,



});

layer.pm.enable({ pinning: true, snappable: true })

您可以使用此代码:

function generateGeoJson(){
    var fg = L.featureGroup();    
    var layers = findLayers(map);
        layers.forEach(function(layer){
        fg.addLayer(layer);
         });
    console.log(fg.toGeoJSON());
}

function findLayers(map) {
    var layers = [];
    map.eachLayer(layer => {
      if (
        layer instanceof L.Polyline || //Don't worry about Polygon and Rectangle they are included in Polyline
        layer instanceof L.Marker ||
        layer instanceof L.Circle ||
        layer instanceof L.CircleMarker
      ) {
        layers.push(layer);
      }
    });

    // filter out layers that don't have the leaflet-geoman instance
    layers = layers.filter(layer => !!layer.pm);

    // filter out everything that's leaflet-geoman specific temporary stuff
    layers = layers.filter(layer => !layer._pmTempLayer);

    return layers;
  }

Fiddle: https://jsfiddle.net/falkedesign/054go8j2/

有关更多信息,请查看 https://github.com/geoman-io/leaflet-geoman/issues/605

附加信息: