openlayers从要素获取图层

时间:2019-01-26 17:21:05

标签: 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());
    });
  });

如何从要素中获取图层?

从2015年开始对this问题的答案似乎很奏效。

我真的必须经历所有这些吗?在OpenLayers 2中,我将引用feature.layer-此功能似乎已消失。

1 个答案:

答案 0 :(得分:0)

感谢@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());
    });
  });