Auto Save on Leaflet Draw

374 views
Skip to first unread message

Shiva Sai

unread,
Aug 24, 2021, 5:22:07 AM8/24/21
to Leaflet
Hi Team,

I'm using Leaflet Draw to display to mark shapes on the leaflet map.

When the existing shape is edited in the map, I'm looking for a way to autosave it rather than requiring the user to click on 'Save' each time.




Leaflet Draw.JPG

Louis Gamor

unread,
Aug 24, 2021, 8:48:49 AM8/24/21
to Leaflet
Hello  Shiva Sai,

You can achieve this by calling your save implementation inside the draw:editresize or L.Draw.Event.EDITRESIZE event listener.

map.on(L.Draw.Event.EDITRESIZE, function(e) {
const layers = e.layers;
layers.eachLayer(function (layer) { 
              // perform your save here   
       });
});

OR

map.on('draw:editresize', function(e) {
       const layers = e.layers;
       layers.eachLayer(function (layer) { 
              // perform your save here   
       });
});

Pat Keller

unread,
Aug 24, 2021, 8:50:58 AM8/24/21
to Leaflet
I think you need to subscribe to draw:edited and/or draw:created events and then call the save function directly. I think it's this.

Shiva Sai

unread,
Aug 24, 2021, 10:20:07 AM8/24/21
to Leaflet
Thank you,

draw:editresize event worked, except that this event doesn't have an array or layers but just has one layer.

The below code worked for me:

map.on(L.Draw.Event.EDITRESIZE, function(e) {
        var resizedLayer = e.layer;       
           //Save function
});

Louis Gamor

unread,
Aug 24, 2021, 10:21:11 AM8/24/21
to leafl...@googlegroups.com
Hello Pat,

Those two events will only be fired onCliok of the save button. And I think that's what Shivasa is trying to avoid.
So editresize should serve him better. 

I'm just uncertain as to why he prefers that use case, because it will keep executing his save logic and making multiple
requests to his db as the user keeps resizing

--

---
You received this message because you are subscribed to the Google Groups "Leaflet" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leaflet-js+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leaflet-js/49912910-ecb9-4177-81f8-ecd1f18e3485n%40googlegroups.com.

Louis Gamor

unread,
Aug 24, 2021, 10:26:40 AM8/24/21
to Leaflet
Ow I see.
How about the draw:editvertex event??

map.on(L.Draw.Event.EDITVERTEX, function(e) {
        var resizedLayer = e.layer;       
           //Save function
});

Try this...
Reply all
Reply to author
Forward
0 new messages