如何在 IE 和 Firefox 中使用 Google 地图 v3 将点击事件绑定到自定义叠加层
我已经在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用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
我知道这是一篇旧文章,但如果您想知道,这里是解决方案:
在上面的代码中,您需要将覆盖函数更改为:
到:
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:
to:
另请注意,尽管即使您使用覆盖窗格,您的单击事件也会在桌面上捕获,但要使触摸事件起作用,鼠标目标窗格是必要的。
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.