Marudi Abhinaya
Marudi Abhinaya

Reputation: 21

map.foreachfeatureatpixel() not working in angular typescript open layers 7

The code working in normal JavaScript finely but in Typescript (Angular) it was throwing an error that this.map.forEachFeatureAtPixel(), this.map is undefined and cannot read properties of undefined.

This is my code :

public selected: Collection<Feature<any>> = new Collection();
multiSelectFeatures(): void {
  this.clearInteractions();
  this.ClearSelection();
  if (this.selected instanceof Collection) {
    this.selected.forEach(function (feature) {
      feature.setStyle(undefined);
    });
    this.selected.clear();
  }
  this.map.on("singleclick", this.getmultiplefeaturesatpixel);
}

getmultiplefeaturesatpixel(evt: { pixel: Pixel }): void {
  this.map.forEachFeatureAtPixel(evt.pixel, (feature): void => {
    if (feature instanceof Feature) {
      const featureArray = this.selected.getArray();
      const selIndex = featureArray.indexOf(feature);
      if (selIndex === -1) {
        this.selected.push(feature);
        console.log(feature);
        // this.selected.getArray().forEach((feat) => feat.setStyle(editOperations.highlightStyle));
      } else {
        this.selected.remove(feature);
        feature.setStyle(undefined);
      }
    }
  });
}

I want to select multiple features on the map from onclick event but when i pass the single click event to another function there this.map was not defined and getting error cannot read properties of undefined

Upvotes: 0

Views: 106

Answers (0)

Related Questions