1

I've added GeoMan to my leaflet map and just wondering if there is a way I can export all the drawn features into JSON. I'm using it for development only so the JSON can go into console.log

I'm just struggling to work it out. This is the only code I have so far

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



});

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

1 Answers1

4

You can use this code:

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/

For more Information look into https://github.com/geoman-io/leaflet-geoman/issues/605

Additional Information:

  • In the next Realease there will be a function to get all layers.
  • pinning is only working with the pro version
  • Geoman Example: https://jsfiddle.net/o1dwu2vg/
Falke Design
  • 10,635
  • 3
  • 15
  • 30
  • Thank you so much for your answer. I looked into more information as well but for some reason the console is outputting all my existing layers instead of only the new ones. I'm not sure if its because of the way the layers are added. (for some reason mini-markdown hates my code) – Sarah Davies Jul 14 '20 at 22:58
  • var villageMarkers = new L.MarkerClusterGroup({ disableClusteringAtZoom: 2, maxClusterRadius: 600 }); villageMarkers.addLayer(villageLayer); – Sarah Davies Jul 14 '20 at 23:00
  • Nevermind! I simply removed "layer instanceof L.Marker ||" since all my existing layers are only markers and I don't want to add new ones anyway for now. Would still be nice to find the issue but thank you so much. – Sarah Davies Jul 14 '20 at 23:07