0

I created an onChange feature event and each time I drag the feature over the map, it console.log its properties.

var newFeature = new ol.Feature({
   geometry: new ol.geom.Point(e_coordinate),
   property: 1
});
newFeature.setId(33);
source.addFeature(newFeature);

I change the property as follows

source.getFeatureById(33).values_.property=2

And if I console.log the property I get 2, which is correct

Finally I add the on change event on the feature

newFeature.on('change',
  function () {
    try {
         console.log("on change props -->" + props);
    } catch (error) {
         alert('feature on change error 2\n' + error);
    }
  },
  newFeature
); 

The problem appears when I drag the feature over the map. It keeps logging the previous value (1) and not the latest one (2). Is there any way to reset the new property some how inside the on change event?

Am I missing something?

4
  • 1
    First, you don't change values by modifying internal feature property .values_. Internal OL properties should never be modifed directly. The correct way is by using .set method: source.getFeatureById(33).set('property', 2). Second, third parameter newFeature in setting change event processing function has no meaning, method .on accepts only two parameters. Third, what do you mean by "keeps logging the previous value"? What is props? Where is it defined? When does it change?
    – TomazicM
    Aug 21, 2021 at 16:43
  • Hi TomazicM and R.Martin! Thanks for your comments. I totaly forgot to add the props variable! The props is a variable that holds the contents of the feature properties. var props=source.getFeatures()[1].values_.properties. I change it from the browser console with the anorthodox mentioned way, just to figure out if the problem ensist. Just to mention, the code has being changed. (:
    – michael
    Aug 22, 2021 at 17:52
  • So you understand now what you have to change in your code?
    – TomazicM
    Aug 22, 2021 at 18:18
  • Yes, but the posted snapshot of this code has been totaly removed.
    – michael
    Aug 23, 2021 at 9:08

1 Answer 1

0

Features properties are useally a key-value pair ( {color : 'red'}) and modifications can be made with setProperties method

var newFeature = new ol.Feature({
  geometry: new ol.geom.Point(e_coordinate),
  property: {custom_val : 1}
 });

And modifying properties :

source.getFeatureById(33).setProperties({custom_val:2});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.