Javascript Google maps drawing events

2019-09-03 09:04发布

问题:

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);
            })
        }
}

回答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:

  • Apply event listener to an editable polygon

  • calculate area of a drawn polygon on google map javascript



回答2:

Try adding the listener with google.maps.event:

google.maps.event.addListener(self.drost, 'set_at', function() {
   console.log('it works!');
});