0

In openlayers 5 I have a vector layer and I try to create code to get the properties of a feature after it is clicked.

This is my code so far

var selectClick = new Select({
  condition: click,
  layers:this.vectorlayer
});

this.olmap.addInteraction(selectClick);    

var selectedFeatures = selectClick.getFeatures();

and then I have tried

selectClick.on('select', ()=>{console.log(selectedFeatures);});

and

selectedFeatures.on('add', function(event) {
  console.log( selectClick.getFeatures());  
});

and I get

ERROR TypeError: arr.indexOf is not a function

in both cases.

What am I doing wrong? My ultimate goal is to do something like selectClick.getFeatures().feature.properties.id, since the geoJSON I am loading, also has some metadata properties in it.

So, how can I get the selected feature ?

Thanks

codebot
  • 517
  • 8
  • 29
  • 55
  • 1
    possible duplicate of [Interactive features in vector layers in openlayers 5](https://stackoverflow.com/questions/51639466/interactive-features-in-vector-layers-in-openlayers-5) – geocodezip Aug 02 '18 at 01:47

1 Answers1

0

Another "whoops" moment. The error had to do with the syntax of the selectClick and how I set the layers var. I set the this.vectorlayer just as a var, but it should be inside an array, hence the array-related, indexOf error.

The code in ol5+angular6 now is

this.vectorsource = new VectorSource({});

this.vectorlayer = new VectorLayer({
  source: this.vectorsource
});

this.view = new OlView({
  center: OlProj.fromLonLat([6.661594, 50.433237]),
  zoom: 2,
});

this.olmap = new OlMap({
  target: 'map',
  layers: [this.tilelayer,this.vectorlayer],
  view: this.view,
  projection: 'EPSG:3857'
});

var selectClick = new Select({
  condition: click,
  layers:[this.vectorlayer]//this should be in an array, you silly codebot
});

this.olmap.addInteraction(selectClick);        

selectClick.on(
  'select', function(){     
    console.log(selectClick.getFeatures().item(0).values_);//   contains metadata properties of geoJSON
  }             
);

Thanks

codebot
  • 517
  • 8
  • 29
  • 55