代码之家  ›  专栏  ›  技术社区  ›  minisaurus

openlayers从功能获取层

  •  0
  • minisaurus  · 技术社区  · 6 年前

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

      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——这个功能似乎已经消失了。

    1 回复  |  直到 6 年前
        1
  •  0
  •   minisaurus    6 年前

    多亏了@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());
        });
      });