134

如何从地图外部触发 Google 地图上标记的onclick事件?

我使用API 的第 3 版。我看过很多版本 2 的教程,但找不到版本 3 的教程。

我有一个包含地图所有标记(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');

感谢您的帮助,如果您需要更多信息,请告诉我!

4

2 回答 2

341

我找到了解决方案!感谢萤火虫;)

//"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');
于 2010-04-28T17:30:00.290 回答
14

For future Googlers, If you get an error similar below after you trigger click for a polygon

"Uncaught TypeError: Cannot read property 'vertex' of undefined"

then try the code below

google.maps.event.trigger(polygon, "click", {});
于 2016-03-29T13:42:47.100 回答