如何在Google Maps V3上触发标记的onclick事件?

Ale*_*exV 125 javascript google-maps google-maps-api-3

如何从地图外部触发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');
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*exV 328

我找到了解决方案!感谢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');
Run Code Online (Sandbox Code Playgroud)

  • @Saboor Awan尝试提出一个具体的问题,因为评论并不是解决这个问题的最好方法. (3认同)
  • 为v3执行此操作会给我`TypeError:a在main.js中未定义(第16行,第894行)`这是什么原因? (3认同)

Erg*_*gec 7

对于未来的Google员工,如果在触发多边形的点击后出现类似下面的错误

"Uncaught TypeError: Cannot read property 'vertex' of undefined"
Run Code Online (Sandbox Code Playgroud)

然后尝试下面的代码

google.maps.event.trigger(polygon, "click", {});
Run Code Online (Sandbox Code Playgroud)

  • 你拯救了我的夜晚。我为此奋斗了3个小时!谢谢你!! (2认同)