【问题标题】:How to trigger the onclick event of a marker on a Google Maps V3?如何在 Google Maps V3 上触发标记的 onclick 事件?
【发布时间】:2011-02-13 10:15:46
【问题描述】:

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

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

我有一个全局数组(名为 markers),其中包含地图的所有标记 (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');

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

【问题讨论】:

    标签: javascript google-maps google-maps-api-3


    【解决方案1】:

    我找到了解决办法!感谢 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');
    

    【讨论】:

    • 点击正在触发,但在我的情况下,信息窗口没有调整到地图,它稍微减少了。
    • @Saboor Awan 试着问一个关于这个的具体问题,因为 cmets 不是解决这个问题的最佳方法。
    • 为 v3 执行此操作给了我TypeError: a is undefined in main.js (line 16, col 894) 这是什么原因?
    • @invot 没有看到代码真的帮不上忙,但是我在快速搜索时发现有人遇到同样的问题...尝试使用像素在 CSS 中设置“popupMapIn”的宽度和高度( px) 而不是百分比 (%)。
    • 此解决方案不适用于集群标记(组标记之一)情况。如果你有,请分享建议。谢谢。
    【解决方案2】:

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

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

    然后试试下面的代码

    google.maps.event.trigger(polygon, "click", {});
    

    【讨论】:

    • 你拯救了我的夜晚。我为此挣扎了3个小时!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 2013-08-25
    • 2011-09-23
    相关资源
    最近更新 更多