I've been trying to set up click event listeners on the google.maps.Map
object that's created by the ui-gmap-google-map
directive from the Angular Google Maps library.
I need to do this dynamically, so it seems (by my brief testing at least) that using the events
parameter on the ui-gmap-google-map
directive won't work (because it seems to "read" the parameter value only once, but maybe I'm wrong about that).
So I decided to use the control
parameter, which augments the object named by the parameter value with the methods getGMap
and refresh
. Here is what my directive usage looks like:
<ui-gmap-google-map center="appCtrl.mapSetup.center"
zoom="appCtrl.mapSetup.zoom"
control="appCtrl.mapSetup">
</ui-gmap-google-map>
Finally, I'm loading the Google Maps API asynchronously, and relying on the GoogleMapApi
service/promise (part of Angular Google Maps) to know when I can start safely dealing with the google.maps.Map
object (e.g. adding event listeners etc.). Here is a small example of what that looks like:
// inside the AppController constructor, see the fiddle linked below
var mapSetup = this.mapSetup;
GoogleMapApi.then(function () {
google.maps.event.addListener(mapSetup.getGMap(), 'click', function () {
alert('hello');
});
So when the page is loaded (here's the fiddle), I get "TypeError: undefined is not a function" because the mapSetup
object hasn't been augmented with getGMap
or refresh
. It seems like it should have been, based on my understanding of the documentation.
I wrapped the addListener
call with $timeout
(the Angular service) using a few hundred millisecond delay, and then it worked because the mapSetup
object had by that time been augmented with getGMap
.
Is there a way to avoid having to use $timeout
with an arbitrary delay to wait until the control
-specified object is actually augmented?