【问题标题】:In first click google map info window not opening, its opening on second click only through ajax call, in V3在第一次点击谷歌地图信息窗口没有打开,它在第二次点击打开只能通过 ajax 调用,在 V3
【发布时间】:2013-05-19 16:18:34
【问题描述】:

我正在使用下面的代码通过 ajax 调用在我的地图中打开一个信息窗口

var openedInfoWindow = null; 
var infoWindow = new google.maps.InfoWindow();

function profileInitialiser(marker, location) {
google.maps.event.addListener(marker, 'click', function(){
        new Ajax.Request('/ajax/', {
        parameters: {'action':'profileInfo', 'id':location.id},
        onSuccess: function(transport){
            addInfoWindow(marker, transport.responseText, location.id);
        }
    });
});
}

function addInfoWindow(marker, content, id) {
  google.maps.event.addListener(marker, 'click', function () {
    if (openedInfoWindow != null) {
        openedInfoWindow.close(); 
    } 
    infoWindow.setContent(content);
    infoWindow.open(this.map, marker);
    openedInfoWindow = infoWindow;  
    google.maps.event.addListener(infoWindow, 'closeclick', function() {
    openedInfoWindow = null;
    });
});
}

上述代码,profileInitialiser 函数在加载谷歌地图中的标记时调用。 第一时间,首先通过ajax调用点击标记,信息窗口的内容作为响应来。 在第二次单击中,信息窗口将打开。下一次,单击加载信息窗口的标记是第一次单击。
有人遇到过这个问题吗?有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: google-maps google-maps-api-3 google-maps-markers infowindow


    【解决方案1】:

    当数据从服务器返回时,您的 AddInfoWindow 函数只是向标记添加一个单击侦听器。您还应该打开包含内容的 infoWindow。

    var openedInfoWindow = null; 
    var infoWindow = new google.maps.InfoWindow();
    
    function profileInitialiser(marker, location) {
    // only do this on the first click
    google.maps.event.addListenerOnce(marker, 'click', function(){
            new Ajax.Request('/ajax/', {
            parameters: {'action':'profileInfo', 'id':location.id},
            onSuccess: function(transport){
                addInfoWindow(marker, transport.responseText, location.id);
            }
        });
    });
    }
    
    function addInfoWindow(marker, content, id) {
      // display the content when the marker is clicked        
      google.maps.event.addListener(marker, 'click', function () {
        if (openedInfoWindow != null) {
            openedInfoWindow.close(); 
        } 
        infoWindow.setContent(content);
        infoWindow.open(this.map, marker);
        openedInfoWindow = infoWindow;  
        google.maps.event.addListener(infoWindow, 'closeclick', function() {
        openedInfoWindow = null;
        });
      });
      // click on the marker to display the newly added infowindow
      google.maps.event.trigger(marker, 'click');
    }
    

    【讨论】:

    • 我遵循了您的代码,但现在单击标记,它的持续信息窗口即将出现(自动触发)。我想要,如果我单击单击标记通过 ajax 加载内容并打开信息窗口。请就此提出建议。
    • 你是否在profileInitialiser中进行了从addListener到addListenerOnce的修改?
    • 这可以解释您报告的问题,进行该更改是否为您解决了问题?
    【解决方案2】:

    为什么要添加两次单击事件侦听器。只需从 addInfoWindow 函数中删除侦听器即可。

    代码如下:

    function addInfoWindow(marker, content, id) {
    
    if (openedInfoWindow != null) {
        openedInfoWindow.close(); 
    } 
    infoWindow.setContent(content);
    infoWindow.open(this.map, marker);
    openedInfoWindow = infoWindow;  
    google.maps.event.addListener(infoWindow, 'closeclick', function() {
    openedInfoWindow = null;
    });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      • 2011-04-29
      相关资源
      最近更新 更多