Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 在第一次点击谷歌地图信息窗口不打开,其打开第二次点击只能通过ajax调用,在V3_Google Maps_Google Maps Api 3_Google Maps Markers_Infowindow - Fatal编程技术网

Google maps 在第一次点击谷歌地图信息窗口不打开,其打开第二次点击只能通过ajax调用,在V3

Google maps 在第一次点击谷歌地图信息窗口不打开,其打开第二次点击只能通过ajax调用,在V3,google-maps,google-maps-api-3,google-maps-markers,infowindow,Google Maps,Google Maps Api 3,Google Maps Markers,Infowindow,我使用下面的代码通过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/', {

我使用下面的代码通过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函数在加载google地图中的标记时调用。 第一次,首先通过ajax调用单击标记,infowindow的内容作为响应出现。
在第二次单击中,信息窗口将打开。下一次,单击载入信息窗口的标记会进入第一次单击
以前有人有过这个错误吗?有人能帮我整理一下这个问题吗?

当数据从服务器返回时,您的AddInfo窗口函数只是在标记中添加了一个单击侦听器。您还应该打开包含内容的信息窗口

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');
}

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

代码如下:

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;
});
}

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