Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Cannot get layers array in openlayers listener

I'm building Angular application with Openlayers 6.5. I need to run function on singleclick event, but somehow function in listener runs with old map data:

@Injectable({
  providedIn: 'root'
})
export class MapService {
  private map: Map;
  //.....
// Runs when loading application  
createNewMap(): void {
         this.map = new Map({pixelRatio: 1});
  }
    createObjectByClick() { // Runs when user clicks on a button
        console.info(this.map.getLayers().getArray()); // Here I get correct array of layers, 20 layers
        this.map.once('singleclick', this.listener);
      }

    const listener = (event) => {
        console.info(this.map.getLayers().getArray()); // Here I get incorrect array, only 2 
                                             //layers, which are added when application load map.
        this.createNewObject(event); // Function does not work, because it uses array of layers which is not correct
      }

createNewObject(params: IconFeatureParams) {
       console.info(this.map.getLayers().getArray()); // Again incorrect array, 2 layers
       ......   // Below code is not working because this.map.getLayers().getArray() has only 2 layers
  }

Map is created when application is just loaded, 2 TileLayers added at that moment. Later, more Vector Layers are loaded. At the time of the problem, map has 20 layers, however in the listener function it's only 2 initial TileLayers. Why code in listener function is running with old data? Sorry, for unclear explanation, I did my best to explain this evil magic :)

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!