vector - openlayers 从特征中获取图层

标签 vector openlayers

我有一个选择交互 - 选择与矢量图层关联的特征。我的目标是编辑要素属性并将其保存回数据库。

  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/

相关文章:

c++ - 按特定顺序迭代数组元素

c++ - 计算 cv::Points2f vector 中的重复项

java - 使用 vector jswing 获取正确的表格标题

javascript - 如何让 openlayers map 显示在 twitter bootstrap 中?

javascript - 如何在 OpenLayers 中获取所选要素的事件或 DOM 元素

javascript - OpenLayers 4 城市坐标中心 map

c++ - 我需要替换 vector 中的一系列元素

c++ - 多维数组的 vector

javascript - 如何从 JSON 文件在 OpenLayers 中绘制点类型几何图形

openlayers - 如何将 OpenLayers 3 与 Proj4js 一起使用