Search code examples
leafletleaflet-geoman

How to get the geojson of all features added by Leaflet-geoman


I'm using Leaflet-geoman to draw circles and polygons in a map.

How can I get the geojson of all features drawn in the map ?


Solution

  • To get all layers of the map you can use this:

    var fg = L.featureGroup();
    map.eachLayer((layer)=>{
       if(layer instanceof L.Path || layer instanceof L.Marker){
        fg.addLayer(layer);
      }
    });
    console.log(fg.toGeoJSON());
    

    If you want only the layers they are used from the plugin:

    var fg = L.featureGroup();
    map.eachLayer((layer)=>{
       if((layer instanceof L.Path || layer instanceof L.Marker) && layer.pm){
        fg.addLayer(layer);
      }
    });
    console.log(fg.toGeoJSON());