0

I'm having a problem with the event listeners provided by Google maps API. The thing is, that some events run, some not. I have a setListeners function which sets the listeners after the polygon overlay is complete. The events I would like to hook are: set_at, insert_at, remove_at and click. Now the click events run correctly, but the others not. What could I do wrong? Here is the code:

self.setListeners = function () {
        //this click event runs correctly
        google.maps.event.addListener(self.map, 'click', function (e) {
            self.clearSelection();
        })

        console.log(self.drost);
        if (typeof self.drost != 'undefined') {
            self.drost.addListener('set_at', function (e) {
                console.log(e.overlay);
            });
            self.drost.addListener('insert_at', function (e) {
                console.log(e.overlay);
            });
            self.drost.addListener('remove_at', function (e) {
                console.log(e.overlay);
            });
            //this click also runs correctly
            self.drost.addListener('click', function(e){
                self.setSelection(self.drost);
            })
        }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Krisztián Dudás
  • 856
  • 10
  • 22
  • is self.drost defined? Assuming 'drost' is the polygon, have you tried adding the listener on the google maps event `overlaycomplete`? https://developers.google.com/maps/documentation/javascript/drawinglayer – JorgeObregon Sep 19 '16 at 14:19
  • @JorgeObregon Yes, the self.drost is initialized in the overlaycomplete event from e.overlay. Also, I call the setListeners at the end of the overlaycomplete callback. – Krisztián Dudás Sep 19 '16 at 14:23

2 Answers2

1

Try adding the listener with google.maps.event:

google.maps.event.addListener(self.drost, 'set_at', function() {
   console.log('it works!');
});
JorgeObregon
  • 3,020
  • 1
  • 12
  • 12
  • btw, this answer could be a comment as well, but a snippet of code will help you more than just a link to google documentation – JorgeObregon Sep 19 '16 at 14:27
  • I tried this method, this also does not work. If I add a console log right after the if, like console.log(self.drost) it logs a google maps polygon object. But the event listener callback does not run when I fire an event. But +1 for the effort! :) – Krisztián Dudás Sep 19 '16 at 14:34
1

The events set_at, insert_at, remove_at need to be added to the path of the polygon, not the polygon itself.

related questions:

Community
  • 1
  • 1
geocodezip
  • 158,664
  • 13
  • 220
  • 245