Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last point from Draw Interaction in Ol3

I am trying to remove the last point in a polygon draw feature on esc key.

The following code do not work. It seems to remove it when it draw the area part, but the vertex is stil there.

var geom :ol.geom.Polygon = null;
draw.on("drawstart",(event) => {
    console.log(event);
    var feature :ol.Feature= event.feature;
    console.log(feature);
    geom = feature.getGeometry<ol.geom.Polygon>();
});

$(document).keyup((e)=> {
    if (e.keyCode === 27) {
        var coords = geom.getCoordinates()[0];
        if(coords.length>1)
            geom.setCoordinates([coords.slice(0, coords.length - 2)]);
    }
});
like image 616
Poul K. Sørensen Avatar asked Mar 10 '15 15:03

Poul K. Sørensen


1 Answers

OpenLayers 3 has a new solution to this with the Draw.removeLastPoint() method. Works like a charm!

draw = new ol.interaction.Draw({
  source:source,
  type: /** @type (ol.geom.GeometryType) */ ('Polygon')
})

draw.on('drawstart',
  function(evt){
    $(document).on('keyup', function(event){
      if(event.keyCode === 27){
        draw.removeLastPoint();
       }
     });
});
like image 161
Ryan Williams Avatar answered Sep 30 '22 06:09

Ryan Williams