0

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

2
  • Try this.map.on("singleclick", this.getmultiplefeaturesatpixel.bind(this));. The context in which your function gets called might be different, that's why this is not what you expect. Commented Jul 27, 2023 at 6:22
  • If I try that, I am unable to unbind the click event whenever it was not necessary I have used this.map.un("singleclick", this.getmultiplefeaturesatpixel.bind(this)); for unbinding but still didn't work @OctavianMărculescu Commented Jul 27, 2023 at 12:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.