I would like to be able to draw a linestring and to be able to undo the last drawn point when the key ESC is pressed.
I used that code when I first wrote my application :
// create the interaction
draw_interaction = new ol.interaction.Draw({
source: vector_layer.getSource(),
type: /** @type {ol.geom.GeometryType} */ ($geom_type.val())
});
// add it to the map
map.addInteraction(draw_interaction);
// when a new feature has been drawn...
draw_interaction.on('drawstart', function(evt) {
var feature = evt.feature;
var geom = feature.getGeometry();
document.addEventListener('keyup', function() {
if (event.keyCode === 27) {
if (geom.getType() === "LineString") {
var coords = geom.getCoordinates();
var len = coords.length;
console.log("undo");
if (len > 1) {
geom.setCoordinates(geom.getCoordinates().slice(0, len - 1));
}
}
}
});
});
You can see a live version on codepen following that link:
http://codepen.io/anon/pen/waPXyG
My problem is when I upgraded from 3.5.0 to 3.6.0, the behaviour changed :
http://codepen.io/anon/pen/LVOrrq
As you can see on the live example, when undoing (pressing ESC) it seems to be working but when you want to add a new point to the linestring, the points I removed are back in place. Looks like a caching behaviour as been added to 3.6.0
I hope someone can help me, it's either a regression or a api modification that I did not notice in the change log.
Answer
Since OpenLayers 3.9.0, this is as simple as using the removeLastPoint
method of the DrawInteraction
. So you can do something like this:
document.addEventListener('keydown', function(e) {
if (e.which == 27)
draw_interaction.removeLastPoint()
});
No comments:
Post a Comment