I'm having this issue with Angular2 + GMaps API. I want to show a spinner preloader while the map loads and set some markers. When using the addListenerOnce IDLE Listener, at the first call the listener takes control of my scope, my variable "this" in my component turns into the map itself, so when I want to execute some function or callback inside the listener, for example, if I write something like:
public isLoaded: boolean = false;
...
google.maps.event.addListener(map, 'idle', () => {
this.isLoaded= true;
});
the variable THIS into the listener (thats should reference to the Component scope) turns into the google map itself, hence this.isLoaded returns Undefined (isLoaded is a property in my Component not map property). It's like the listener takes control of my component scope,
the strange thing is that this occurs once, when the component loads the first time, then it fixes and everything works just fine.
Any solution? Sorry for my english. Thanks in advance!
In your snippet context of function execution sets explicitly by Gmaps like this callback.apply(this, args)
So for your case you just need to also set context explicitly, for this purpose Function has .bind() method (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Function/bind)