question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Delete last vertex drawn of polygon

See original GitHub issue

I am creating a polygon using leaflet.draw plugin in mapbox. My requirement is to restrict the user to draw outside a particular boundary and in case user clicks outside that boundary ,last drawn vertex should get deleted and line drawn to that vertex should also get removed. I am writing following code to achieve this.

mapData=document.getElementById('mapTest');

        L.mapbox.accessToken = 'pk.eyJ1Ijoicml0aWthIiwiYSI6IlpnRThghmhbifQ.i9VWs0WDpjyxMn1YFzKOow';
        var map = L.mapbox.map(mapData, 'abc.j565ghh0o')
            .setView( [38.893596444352134, -77.0381498336792], 12);
        featureGroup = L.featureGroup().addTo(map);

drawControl = new L.Control.Draw({
      edit: {
        featureGroup: featureGroup
      },
      draw: {
        polygon: true,
        polyline: false,
        rectangle: false,
        circle: false,
        marker: false
      }
    }).addTo(map);

        map.on('draw:created', showPolygonArea);
        map.on('draw:edited', showPolygonAreaEdited);           

        function showPolygonAreaEdited(e) {
          e.layers.eachLayer(function(layer) {
            showPolygonArea({ layer: layer });
          });

        }

        function showPolygonArea(e) {
          featureGroup.clearLayers();
          featureGroup.addLayer(e.layer);

          var layer = e.layer;



              latLngs = layer.getLatLngs();

              mapZoom=map.getZoom();

        }


 map.on('click', function(e) {

                var currentLoc=e.latlng;
                if(polygonParent!="")
                    {
                    var a=polygonParent.getBounds();
                     containsCurrentLocation=a.contains(currentLoc);
                    }


                if(containsCurrentLocation==false)
                    {
                    alert("Select point within the boundary.");
                    // how to remove last vertex here??                 }

            });

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
germanjoeycommented, Dec 15, 2016

Wait until the next version, it will be easier then. You’ll be able to do something like this:

var drawHandler = null;
map.on(L.Draw.Event.DRAWVERTEX, function(e) {
    drawHandler = e.drawHandler;
});

function deleteLastDrawnVertex () {
    if (drawHandler && drawHandler.enabled()) {
        drawHandler.deleteLastVertex();
    }
    else {
        throw new Error('There is no vertex to delete!');
    }
}

Note that the call to deleteLastVertex will trigger an L.Draw.Event.DRAWVERTEX event.

For edit mode, it’s a little tricker. It sounds like you’ve found the marker you want to delete; in that case, you can do this:

var editHandlerIndex = {};
map.on(L.Draw.Event.EDITHOOK, function(e) {
    if (e.vertex) {
        editHandlerIndex[e.layer._leaflet_id] = e.editHandler;
    }
});
map.on(L.Draw.Event.EDITVERTEX, function(e) {
    editHandlerIndex[e.poly._leaflet_id] = e.editHandler;
});

function deleteVertex(marker) {
    if ((!marker) && (!marker.enabled()) {
        throw new Error("Can't delete a phantom vertex!);
    }

    map.once(L.Draw.Event.EDITVERTEX, function (e) {
        var editHandler = editHandlerIndex[e.poly._leaflet_id];
        editHandler.updateMarkers();
        e.poly.fire('edit');
    });
    marker.fire('click');
}

The tricky parts are making sure you have the right edit handler, and then updating the markers once the marker click has finished firing. And again, this is only once the new version is released.

On Thu, Dec 15, 2016 at 6:13 AM, mikila85 notifications@github.com wrote:

+1

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Leaflet/Leaflet.draw/issues/321#issuecomment-267336760, or mute the thread https://github.com/notifications/unsubscribe-auth/AAzNEtTwxe0vntYCbEGEFAR3_si3Ru6yks5rIUrygaJpZM4CgK-i .

0reactions
cavasinfcommented, Jul 21, 2021

@germanjoey There’s no e.drawHandler from the event L.Draw.Event.DRAWVERTEX. Where did you find this ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deleting last vertex using ArcMap Keyboard Shortcut?
As I was working on ArcMap 10.1 and 10.2 there was also a Backspace key working to delete last vertices, while continuing drawing....
Read more >
Deleting a Vertex | Maps JavaScript API - Google Developers
This example demonstrates how a user can delete a vertex, or a point on the line, by right-clicking on a vertex to show...
Read more >
Delete a vertice of polygon on edited shape - Microsoft Learn
If the button is clicked, delete the vertex from the shape by getting all the positions/coordinates in the shape as an array (getCoordinates) ......
Read more >
Leaflet.draw delete last point - Stack Overflow
Call the deleteLastVertex method on your draw instance: var drawPolyline = new L.Draw.Polyline(map, drawControl.options.polyline).enable(); ...
Read more >
Deleting a vertex—ArcMap | Documentation
Deleting a vertex · Click the Delete Vertex tool Delete Vertex Tool on the Edit Vertices toolbar and drag a box around the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found