angular - Method to get the corresponding layer of a given feature:source.Vector.getSource() is not a function -


i using angular , openlayers3 include map in web-application.

i trying find out layer given feature belongs to. doing because want remove selected feature map. have different layers different features, cannot sure layer have remove feature from.

here important lines understand code-extract:

import vectorlayer 'ol/layer/vector'; import vectorsource 'ol/source/vector'; import feature 'ol/feature'; [...] map: ol.map; 

my method looks this:

  get_layeroffeature(feature: feature): vectorsource {     //iterate on layers of map     (let x of this.map.getlayers().getarray()) {       let source: vectorsource = x.getsource();       let features: feature[] = source.getfeatures();       //iterate on features of vector-source       (let y of features) {         if (y == feature) {           return x;         }       }     }     //given features not belong existing vector-source     return null;   } 

my current problem compiler complains line:

let source: vectorsource = x.getsource(); 

it gives me following error:

property 'getsource' not exist on type 'base'

the problem seems .getlayers() returns collection of bases. using .getarray() array of bases.

then had @ docs: openlayers docs: base

this class abstract class , not include function "getsource". yet, there subclass "ol.layer.layer" looking for. class includes getsource() method want use.

what tried:

casting x-element of for-each-loop , saving new variable:

let x2: vectorlayer = <vectorlayer>x; let source: vectorsource = x2.getsource(); 

at least did not throw error during compilation. however, following runtime-error:

error typeerror: source.getfeatures not function

is problem did not cast base-object vector-source-object properly? looks object (in case layer) missing required method though supposed there according docs.

explanation comments:

get_layeroffeature(feature: feature): vectorsource {   let id = feature.getid();   (let x of this.map.getlayers().getarray()) {     if(x instanceof vectorlayer) {       let source: vectorsource = x.getsource();       let feat: feature = source.getfeaturebyid(id);       if(feat !== undefined && feat !== null) {         return source;       }     }   }   return null; } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -