简体   繁体   English

openlayers从要素获取图层

[英]openlayers get layer from feature

I have a select interaction - to select features associated with a vector layer. 我有一个选择交互-选择与矢量层关联的要素。 My goal is to edit the feature attributes and save back to a database. 我的目标是编辑要素属性并将其保存回数据库。

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

How do I get the layer from the feature? 如何从要素中获取图层?

The answer to this question from 2015 would appear to work. 从2015年开始, 这个问题的答案似乎奏效了。

Do I really have to go through all this? 我真的必须经历所有这些吗? In OpenLayers 2, I would refer to feature.layer - this functionality appears to have gone. 在OpenLayers 2中,我将引用feature.layer-此功能似乎已消失。

Thanks to @Mike, I added me.select.getLayer(feature) in the loop over the features. 感谢@Mike,我在me.select.getLayer(feature)中的循环中添加了me.select.getLayer(feature)

Full solution is: 完整的解决方案是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM