geoman-io / leaflet-geoman

🍂🗺️ The most powerful leaflet plugin for drawing and editing geometry layers
https://geoman.io
MIT License
2.21k stars 433 forks source link

How to delete all another polygons? #431

Closed vodnicearv closed 5 years ago

vodnicearv commented 5 years ago

Hello, need to remove all another polygons when user add a new polygon on map. Or limit count of polygons to 1. Can someone tell me how to do this?

this.map.on('pm:create', e => {
     // I listen the create event
});

I tried: this.map.pm.Draw.Polygon._removeLastVertex() but get an error: Screenshot_2

codeofsumit commented 5 years ago

You can do this with regular leaflet. Something like this:

this.map.on('pm:create', e => {
  this.map.eachLayer(layer, () => {
    if(!e.target !== layer) {
      layer.remove();
    }
  })
});
vodnicearv commented 5 years ago

@codeofsumit Thank You!

My working code is:

this.map.eachLayer(layer => {
  if (typeof layer._latlngs !== 'undefined' && layer._latlngs.length > 0 && e.layer._leaflet_id !== layer._leaflet_id) {
    layer.remove()
  }
})