Leaflet / Leaflet.Editable

Make geometries editable in Leaflet.
http://leaflet.github.io/Leaflet.Editable/doc/api.html
559 stars 197 forks source link

How to cancel polyline drag event #161

Open parky128 opened 6 years ago

parky128 commented 6 years ago

When editing a polyline, is it possible to prevent the user dragging certain points along the line? In my case I wish to prevent the user moving the start and end points. I know I can hook onto the drag start event and figure out whether the point is at the start or end of the line being edited, but I have been unable to cancel the drag event altogether. Is there a way?

fabric-io-rodrigues commented 6 years ago

Hi @parky128, is possible.

use event: editable:vertex:dragstart and dragging.disable,

See sample to prevent drag first and last point of line:

map.on('editable:vertex:dragstart', function (e) {
    var listLatLngs = e.vertex.latlngs;
    var firstLatLng = listLatLngs[0];
    var lastLatLng = listLatLngs[listLatLngs.length-1];
    var currentLatLng = e.vertex.latlng;

    if ((currentLatLng.lat == firstLatLng.lat && currentLatLng.lng == firstLatLng.lng) ||
        (currentLatLng.lat == lastLatLng.lat && currentLatLng.lng == lastLatLng.lng))
    {
        e.vertex.dragging.disable();
    }

});

Hope this helps. have a nice day. Fabricio