从Ol3中的Draw Interaction中删除最后一个点

Pou*_*sen 5 javascript openlayers-3

我试图删除esc键上多边形绘制功能的最后一个点.

以下代码不起作用.它似乎在绘制区域部分时将其删除,但顶点是在那里.

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)]);
    }
});
Run Code Online (Sandbox Code Playgroud)

小智 4

OpenLayers 3 有一个新的解决方案,即 Draw.removeLastPoint() 方法。奇迹般有效!

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();
       }
     });
});
Run Code Online (Sandbox Code Playgroud)