Open Layers 3: How to create listener for a modify interaction

白昼怎懂夜的黑 提交于 2019-11-30 17:11:45

问题


I have managed to set up a modify interaction.

The docs for ol.interaction.Modify (http://ol3js.org/en/master/apidoc/ol.interaction.Modify.html) don't metion a single event that is fired when a feature is modified.

Unlike for instance for ol.interaction.Draw (http://ol3js.org/en/master/apidoc/ol.interaction.Draw.html) which works nicely.

I need to update the coordinates in the database when a feature has been modified.

How can I set up a listener?


回答1:


I found a solution.

The high-level-explanation is here: http://boundlessgeo.com/2014/06/openlayers-editing-wfs-t/

Basically you don't listen to changes in the modify-interaction (like you do in the draw-interaction). Instead, you listen to changes in the selected features themselves.

Here's a short extract:

// get the features from the select interaction
var selected_features = select_interaction.getFeatures();
// when a feature is selected...
selected_features.on('add', function(event) {
    // get the feature
    var feature = event.element;
    // ...listen for changes on it
    feature.on('change', function(event) {
        // got it!
    });
});

Here is a complete working example: http://codepen.io/barbalex/pen/fBpyb




回答2:


for this, you have to use like following :

feature.once('change', function(bool) {if (bool) {document.getElementById('my_input_text').value='feature changed'}})

you have to use 'once' instead of 'on' to avoid multiple check and use boolean variable to check if feature is changed or not.

Hope this help



来源:https://stackoverflow.com/questions/24726734/open-layers-3-how-to-create-listener-for-a-modify-interaction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!