我有一个选择交互 - 选择与矢量图层关联的特征。我的目标是编辑要素属性并将其保存回数据库。
import Map from 'ol/Map';
import View from 'ol/View';
import Select from 'ol/interaction/Select.js';
...
this.map = new Map({
target: 'map',
view: new View({
center: this.$root.mapState.center,
zoom: this.$root.mapState.zoom
})
});
AddLayers(this.map, this.$root.map.layers, this.$root.register);
this.select = new Select();
this.map.addInteraction(this.select);
this.select.on('select', function(e) {
e.target.getFeatures().forEach(function(feature) {
alert('Selected ' + feature.getId());
});
});
如何从功能中获取图层?
this的答案2015 年的问题似乎有效。
我真的必须经历这一切吗?在 OpenLayers 2 中,我会引用 feature.layer - 此功能似乎已经消失。
最佳答案
感谢@Mike,我在功能循环中添加了 me.select.getLayer(feature)
。
完整的解决方案是:
import Map from 'ol/Map';
import View from 'ol/View';
import Select from 'ol/interaction/Select.js';
...
this.map = new Map({
target: 'map',
view: new View({
center: this.$root.mapState.center,
zoom: this.$root.mapState.zoom
})
});
AddLayers(this.map, this.$root.map.layers, this.$root.register);
this.select = new Select();
this.map.addInteraction(this.select);
var me = this;
this.select.on('select', function(e) {
e.target.getFeatures().forEach(function(feature) {
var layer = me.select.getLayer(feature);
alert('Selected ' + feature.getId());
});
});
关于vector - openlayers 从特征中获取图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54380785/