简体   繁体   English

无法在 Google 地图信息框上添加事件

[英]Unable to add event on Google map Infobox

I am using Infobox with Google Map V3 (attached image).我正在将 Infobox 与 Google Map V3(附图)一起使用。 While clicking on Infobox, I want to go to a details page.单击信息框时,我想转到详细信息页面。 But I am not able to add a click listener on the infobox.但我无法在信息框上添加点击侦听器。 Here is the code I am using:这是我正在使用的代码:

在此处输入图片说明

This is my infobox config:这是我的信息框配置:

    var ib = new InfoBox({
        alignBottom : true,
        disableAutoPan: false,
        maxWidth: 0,
        pixelOffset: new google.maps.Size(-125, -50),
        zIndex: null,
        closeBoxURL: "",
        infoBoxClearance: new google.maps.Size(1, 1),
        isHidden: false,
        pane: "floatPane",
        enableEventPropagation:false
    });

And I added listener to this infobox like this:我向这个信息框添加了监听器,如下所示:

    google.maps.event.addListener(ib, 'domready', function() {
        if(Ext.get(ib.div_)){
            Ext.get(ib.div_).on('click', function(){
                console.log('test on click')
            }, this);

            Ext.get(ib.div_).on('mousedown', function(){
                console.log('test on click')
            }, this);
        }
    });

While enableEventPropagation = false , the event doesn't propagate to MAP but no event is working on the infobox.虽然enableEventPropagation = false ,事件不会传播到 MAP,但没有事件在信息框上工作。

While enableEventPropagation = true , the events (click, mousedown) works but clicking on other part of the infobox, it takes me to map or another marker.虽然enableEventPropagation = true ,事件(点击,鼠标按下)有效,但点击信息框的其他部分,我需要映射或其他标记。

Any idea how to solve this?知道如何解决这个问题吗?

You need to add domready event to infobox's eventListener not to google maps' one.您需要将domready事件添加到domready的 eventListener 而不是 google maps 的 eventListener。 Only after infobox's html is on screen, you can bind event.只有当 infobox 的 html 出现在屏幕上后,您才能绑定事件。 To prevent multi event binding, close other infobox before you load a new one.为防止多事件绑定,请在加载新信息框之前关闭其他信息框。

infobox= new InfoBox();
google.maps.event.addListener(marker, 'click', function() {
  infobox.close();
  infobox= new InfoBox({ ... });
  infobox.open(map, this);
  infobox.addListener("domready", function() {
    $("#target").on("click", function(e) { /* do something */ });
  });
});

You can attach a listener to whatever is the content of your InfoBox.您可以将侦听器附加到 InfoBox 的任何内容。

var boxText = document.createElement('div'); 
var myOptions = {
  content: boxText,
   ... 
}
var ib = new InfoBox(myOptions);
ib.open(theMap, marker);
boxText.setAttribute('onclick', 'alert("InfoBox clicked")');

Would that work for you ?那对你有用吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM