Poul K. Sørensen Poul K. Sørensen - 1 year ago 173
Javascript Question

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) => {
var feature :ol.Feature= event.feature;
geom = feature.getGeometry<ol.geom.Polygon>();

$(document).keyup((e)=> {
if (e.keyCode === 27) {
var coords = geom.getCoordinates()[0];
geom.setCoordinates([coords.slice(0, coords.length - 2)]);

Answer Source

You need to use a setCoordinates on the geometry element, after removing the last segment.

Here is an example:

  function (evt) {
    $(document).on('keyup', function (event) {
      if (event.keyCode === 27) {
        var feature = evt.feature;
        var geom = feature.getGeometry();
        if (geom.getType() === LINE_STRING) {
          geom.setCoordinates(geom.getCoordinates().slice(0,geom.getCoordinates().length - 1));
  }, this);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download