重新加载地图时google.maps.event.trigger无法运行 [英] google.maps.event.trigger not run when map reload

查看:73
本文介绍了重新加载地图时google.maps.event.trigger无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Google Maps api v3的项目,并且我正在使用google.maps.event.trigger来调用或触发在地图加载时创建的侦听器.在这种情况下,我将监听器设置为单击多边形,因此单击多边形时,它将打开相关的信息窗口,并且在同一JavaScript文件中的另一个功能块上设置了google.maps.event.trigger. 重新加载整个Web程序时,它运行良好.但是,当我重新加载地图或使用具有相同变量对象的多边形重新初始化地图时,语法google.maps.event.trigger无法运行.这是我正在使用google.maps.event.trigger

I have a project using Google maps api v3, and I am using google.maps.event.trigger to call or trigger the listener that is created when the map loads. In this case I set listener at polygon click, so when polygon is clicked, it opens the relevant infowindow and google.maps.event.trigger is set on another block of function in the same javascript file. It is running well when the whole web program is reloaded. But when I reload the map or map is re-initialized with the polygon with same variable object, the syntax google.maps.event.trigger does not run. This is my function that is using google.maps.event.trigger

 function callInfoPoly(nomor){
    alert(polygon[nomor].getPath().getAt(0));
    google.maps.event.trigger(polygon[nomor], "click");
    }

警报运行良好,这意味着变量包含多边形对象,但google.maps.event.trigger无法运行.

the alert runs well, it means the variable contains the polygon object, but google.maps.event.trigger doesn't run.

推荐答案

触发点击后进入控制台,您会看到一个错误:

When you take a look into the console after triggering the click you'll see an error:

Uncaught TypeError: Cannot read property 'vertex' of undefined 

此未定义对象是 PolyMouseEvent .当实际点击发生时,该对象将传递给点击回调.但是,当您以编程方式触发单击时,会丢失该对象,这会导致上面的错误(并在重新初始化地图后以某种方式停止了单击侦听器的绑定).

This undefined object is a PolyMouseEvent . This object will be passed to the click-callback when a real click occurs. But when you trigger the click programmatically this object is missing, what forces the error above(and somehow stops the binding of the click-listener after re-initializing the map ).

解决方案:以编程方式触发点击时,将空对象作为参数传递给点击回调:

solution: pass an empty object as argument to the click-callback when you trigger the click programmatically:

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

演示: http://jsfiddle.net/doktormolle/jVFN2/

这篇关于重新加载地图时google.maps.event.trigger无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆