Javascript Google maps drawing events

天大地大妈咪最大 提交于 2019-12-12 04:22:37

问题


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


来源:https://stackoverflow.com/questions/39575408/javascript-google-maps-drawing-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!