简体   繁体   中英

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.

Do I really have to go through all this? In OpenLayers 2, I would refer to feature.layer - this functionality appears to have gone.

Thanks to @Mike, I added me.select.getLayer(feature) in the loop over the features.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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