2012-07-11 57 views
2

我知道这是一个重复的问题,如何在Google map V3中同时打开单个Info窗口?

我在我的Django基于Web的应用程序上使用谷歌地图v3。我在哪里使用Markers, Infowindow and polyline。一切正常,除了当我点击一个标记来显示内容的信息窗口,前一个打开的信息窗口没有关闭。

我张贴了我的地图代码(只有脚本的一部分或者是有用的):

var marker = add_marker(flightPlanCoordinates[i][0], flightPlanCoordinates[i][1],"Event Detail",myHtml); 

这里myHtml是包含信息窗口内容的变量。我没有在这里定义变量。 SO忽视它。

marker.setMap(map); 
    } 

    var flightPath = new google.maps.Polyline({ 
       path: flightPlanCoordinatesSet, 
       strokeColor: "#FF0000", 
       strokeOpacity: 1.0, 
       strokeWeight: 2 
        }); 
    flightPath.setMap(map); 
} 

function add_marker(lat,lng,title,box_html) { 
var infowindow = new google.maps.InfoWindow({ 
    content: box_html 
}); 

var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(lat,lng), 
     map: map, 
     title: title 
}); 

google.maps.event.addListener(marker, 'click', function() { 
    infowindow.open(map,this); 
}); 

return marker; 
} 

回答

2

而不是多个infoWindows只使用一个实例。

单击标记时,先关闭infoWindow,然后设置新内容并打开infoWindow。

function add_marker(lat,lng,title,box_html) 
{ 
    //create the global instance of infoWindow 
    if(!window.infowindow) 
    { 
    window.infowindow=new google.maps.InfoWindow(); 
    } 

    var marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(lat,lng), 
     map: map, 
     title: title 
    }); 

    google.maps.event.addListener(marker, 'click', function() { 
    infowindow.close(); 
    infowindow.setContent(box_html); 
    infowindow.open(map,this) 
    }); 

    return marker; 
} 
+0

感谢您的回复,我尝试过使用此选项。但没有工作。可能是我错了,你可能会显示我添加侦听器方法的伪代码 – 2012-07-11 08:20:22

+1

修改后的add_marker函数应该可以工作。 – 2012-07-11 08:40:59

+0

@ Dr.Molle如果我创建infoWindow的全局实例,我在'LatLngBounds'中有多个标记,最后一个infowinodow显示所有的指针 – 2015-07-08 12:01:24

相关问题