如何在 IE 和 Firefox 中使用 Google 地图 v3 将点击事件绑定到自定义叠加层

发布于 2024-11-25 12:02:14 字数 698 浏览 0 评论 0原文

我已经在google文档的指导下对我的overlay对象进行了子类化,我的onAdd()函数如下所示:

MyOverlay.onAdd() {
    var div_parent = document.createElement("DIV");
    var div_child = document.createElement("DIV");
    div_child.innerHTML = "Click Me";
    div_parent.appendChild( div_child );
    this.getPanes().overlayLayer.appendChild(div_parent);
    var this = that;
    google.maps.event.addDomListener( div_parent, 'click', function(){
        google.maps.event.trigger(that, 'click'); // from [http://stackoverflow.com/questions/3361823/make-custom-overlay-clickable-google-maps-api-v3]
        alert("Clicked");
    } );

}

我的代码只能在IE中运行良好,但在Firefox和Chrome中,点击事件将不再被触发。

那么如何解决问题呢?

I've already subclass my overlay object under the instruction of google document, and my onAdd() function is listed below:

MyOverlay.onAdd() {
    var div_parent = document.createElement("DIV");
    var div_child = document.createElement("DIV");
    div_child.innerHTML = "Click Me";
    div_parent.appendChild( div_child );
    this.getPanes().overlayLayer.appendChild(div_parent);
    var this = that;
    google.maps.event.addDomListener( div_parent, 'click', function(){
        google.maps.event.trigger(that, 'click'); // from [http://stackoverflow.com/questions/3361823/make-custom-overlay-clickable-google-maps-api-v3]
        alert("Clicked");
    } );

}

My code can work well ONLY in IE, but in Firefox and Chrome, the click event will not be triggered anymore.

So how to solve the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿看小鸭子会跑 2024-12-02 12:02:14

您应该使用overlayMouseTarget,而不是使用overlayLayer mapPanes。

参考:http://code.google.com/apis/maps/documentation/javascript/ overlays.html#CustomOverlays

Instead of using overlayLayer mapPanes, you should use overlayMouseTarget.

Reference: http://code.google.com/apis/maps/documentation/javascript/overlays.html#CustomOverlays

夏の忆 2024-12-02 12:02:14

我知道这是一篇旧文章,但如果您想知道,这里是解决方案:

在上面的代码中,您需要将覆盖函数更改为:

this.getPanes().overlayLayer.appendChild(div_parent);

到:

this.getPanes().overlayMouseTarget.appendChild(div_parent);

I know this is an old post, but if you were wondering, here is the solution:

In your code above, you need to change the overlay function from:

this.getPanes().overlayLayer.appendChild(div_parent);

to:

this.getPanes().overlayMouseTarget.appendChild(div_parent);

神回复 2024-12-02 12:02:14

另请注意,尽管即使您使用覆盖窗格,您的单击事件也会在桌面上捕获,但要使触摸事件起作用,鼠标目标窗格是必要的。

Also note although your click events will be captured on desktop even if you use overlay pane, for touch events to work, mouse target pane is necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文