Javascript 如何在 Google Maps V3 上触发标记的 onclick 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2730929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trigger the onclick event of a marker on a Google Maps V3?
提问by AlexV
How do I trigger the onclickevent of a marker on a Google Maps from outside the map?
如何从地图外部触发Google 地图上标记的onclick事件?
I use version 3of the API. I've seen many tutorials for version 2, but can't find this for version 3.
我使用API 的第 3 版。我看过很多版本 2 的教程,但找不到版本 3 的教程。
I have a global array (named markers) containing all the marker of the map (google.maps.Marker). Now I want do do something like:
我有一个全局数组(命名为标记),其中包含地图的所有标记(google.maps.Marker)。现在我想做一些类似的事情:
markers[i].click(); //I know it's not working, but you get the idea...
//Next line seems to be the way in v2, but what's the equivalent in v3?
GEvent.trigger(markers[i], 'click');
Thanks for your help and if you need more info, let me know!
感谢您的帮助,如果您需要更多信息,请告诉我!
回答by AlexV
I've found out the solution! Thanks to Firebug ;)
我找到了解决办法!感谢 Firebug ;)
//"markers" is an array that I declared which contains all the marker of the map
//"i" is the index of the marker in the array that I want to trigger the OnClick event
//V2 version is:
GEvent.trigger(markers[i], 'click');
//V3 version is:
google.maps.event.trigger(markers[i], 'click');
回答by Ergec
For future Googlers, If you get an error similar below after you trigger click for a polygon
对于未来的 Google 员工,如果您在触发多边形点击后出现类似以下错误
"Uncaught TypeError: Cannot read property 'vertex' of undefined"
then try the code below
然后试试下面的代码
google.maps.event.trigger(polygon, "click", {});

