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 :)