user0129e021939232
user0129e021939232

Reputation: 6355

leaflet.pm edit mode get new coordinates within shape

I'm using leaflet js to build a map with some pins https://leafletjs.com/ and I'm also allowing drawing of shapes, e.g. polygons, circles etc. I also these to be edited using a plugin called leaflet.pm https://github.com/codeofsumit/leaflet.pm.

There are events here but none of the events are giving be back the cordinates of the new position after edit mode has been disabled or after the drag has finished. This is the event I have hooked into;

map.on('pm:globaleditmodetoggled', function(e) {
    console.log(e);
});

Wheres this event gives me what is required;

map.on('pm:create', function(e) {
    let obj = {
        type: e.shape,
        coordinates: e.layer.editing.latlngs[0][0]
    };

    $('#cords').val(JSON.stringify(obj))
});

Any ideas how when the shape is edited that I can get the update coordinates?

Upvotes: 2

Views: 4198

Answers (1)

ProblemsOfSumit
ProblemsOfSumit

Reputation: 21325

I'm Sumit, the maintainer of leaflet.pm

What you can do is: listen to an event being created and add the edit event to the new shapes:

map.on('pm:create',(e) {
  e.layer.on('pm:edit', ({ layer }) => {
    // layer has been edited
    console.log(layer.toGeoJSON());
  })
});

Of course, whenever you add a layer to the map you can also apply the pm:edit event to its reference. Additionally, when you create layers or add layers to the map, you can simply store the reference. When editing is done you can just check the reference for it's coordinates (just as you would normally in leaflet). If you just need to know when editing is done, use the pm:edit event to catch whenever a layer was edited.

Hope this helps 👍

Upvotes: 4

Related Questions