2

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 ?

Kleyson Rios
  • 2,597
  • 5
  • 40
  • 65

2 Answers2

3

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());
Falke Design
  • 10,635
  • 3
  • 15
  • 30
0

I suggest to use a custom leaflet featureGroup which can be fed to geoman. Let's say you are drawing polygons

const yourCustomPolygonLayer = L.featureGroup().addTo(map);

map.pm.setGlobalOptions({
    layerGroup: yourCustomPolygonLayer
});

Now you can iterate over yourCustomPolygonLayer easily.

yourCustomPolygonLayer.eachLayer(layer => {
    console.info(layer._latlngs)
})
Timothy Dalton
  • 1,290
  • 2
  • 17
  • 24