I think there is a problem with freehandCondition
in Draw
interaction, which is ShiftKeyOnly
by default. When I changed it to ol.events.condition.never
I can delete vertex without creating new feature. This solution deactivates freehand drawing.
Example:
draw = new ol.interaction.Draw({
features:features,
type: ('Polygon'),
freehandCondition: function(event){
return ol.events.condition.never(event);
}
I have managed to find a part solution to my issue. Note that my first example site did not show the real issue, as the source was always loaded by the time the user clicks on a button. The problem I have mostly occurs when I am trying to do someting before the (asynchronous) load of features.
My new example site is http://mapping4ops.org/ShowMapsDev/foreachfeature2.html. This is similar to the first one, except that if you call it with a parameter (eg ?H=2) it does an immediate Hide using the given numbered Hide function, but you can still Click on theHide/Unhide buttons to try them.
Using the same functions as before, the results are that:
- Hide1 [getSource and getFeatures] does NOT work in immediate mode, but does work using the
button
- Hide2 [getSource and forEachFeature] does NOT work in immediate mode, but does work using the button
- Hide3 [.getSource().once('change') and forEachFeature] does work in immediate mode, but does NOT work using the button (although it will later fire if there is any change to the source, eg by clicking on the Hide1 button)
- Hide4 [.getSource().on('change') and forEachFeature] does NOT work properly in immediate mode (it gets into a loop), and something similar happens when using the button
So I have created Hide5 which combines the best of these (2 and 3), and this works both in immediate mode, and using the button. You can try it at http://mapping4ops.org/ShowMapsDev/foreachfeature2.html?H=5, and also then clicking on Hide5 and Unhide5.
The code for Hide5 (without the console.log statements) is
function Hide5(hide){ // hide is boolean, TRUE means hide alternate features, FALSE unhide all
var source = vectorLayer.getSource();
var numFeatures = source.getFeatures().length;
if (numFeatures==0) {
vectorLayer.getSource().once('change', function(evt){
source = evt.target;
if (source.getState() === 'ready') {
doHide5Actions(source,hide);
}
});
} else {
if (source.getState() === 'ready') {
doHide5Actions(source,hide);
}
}
}
Note this will not work properly if the number of features is really zero.
A problem with this approach is that when I try to do more than one action updating features (eg a Hide and then also an Unhide immediately, using http://mapping4ops.org/ShowMapsDev/foreachfeature2.html?H=5&U=Y. [The &U=Y here gets it to Unhide after the Hide.]) the behaviour is complicated.
Note that it may seem to silly to Hide and Unhide straight away but these are just simple examples of changing the feature properties. In my full application I am doing other things.
What seems to happen is that both actions get set up using .once, and then both get triggered. The first action set up (the Hide) seems to get processed after the second (the Unhide); and an error is triggered in ol-debug (something to do with a listener not being defined).
I shall keep trying to understand and solve this, but if someone can tell me a universal solution I would be grateful. It feels like functionality that many would need, but maybe I am going about it the wrong way?
Following on this research I have now implemented a solution in my real application (not the example I have been using). This involves combining all the processes I want to do to the features into the one routine, with options. Then I can call it just as doHide5Actions is called above. This works fine, both immediately and on user input, without any errors.
Best Answer
Instead of
you just need to use the following
For the docs reference, go to https://openlayers.org/en/latest/apidoc/module-ol_Object-BaseObject.html#unset