crisley
crisley

Reputation: 23

How to be notified when a Feature geometry is changed?

The documentation for ol.Feature OL3 API states that it fires a 'change:geometry' event. The documentation does not specify when this event is fired.

The following JSFiddle has an example of moving vector features using an ol.interaction.Translate to move the features. While a feature is being moved, a 'change' event is fired, but never a 'change:geometry' event.

What is the correct way to be informed that a Features geometry has been updated?

Code:

var select = new ol.interaction.Select();

select.on('select', (event) => {
  event.selected.forEach((selectedFeature) => {
      selectedFeature.on('change', (event) => {console.log('change', event)})
      selectedFeature.on('change:geometry', (event) => {console.log('change:geometry', event)})
  })
})

var translate = new ol.interaction.Translate({
  features: select.getFeatures()
});

Upvotes: 2

Views: 3032

Answers (1)

Alexandre Dubé
Alexandre Dubé

Reputation: 2829

The change:geometry event occurs at the level of the feature if you change the geometry object, i.e. if you did: feature.setGeometry(aNewGeomObj), feature.setGeometry(null), etc.

What you're looking for is listening the change event of the geometry object itself. You can do this like this:

var geometry = feature.getGeometry();
geometry.on('change', function(evt) {
  console.log(evt)
}, this);

You should be able to do what you want from there.

Upvotes: 3

Related Questions