我之前使用的是InfoWindow。即使我点击了很多标记,它也只能显示一个InfoWindow。
但是当我更改它并用InfoBubble替换它时,它会显示多个气泡。它不会自动关闭之前打开的泡泡,而是保持打开状态,它们似乎弄乱了我的地图。
我不知道该怎么办这个问题。这是我的代码。
downloadUrl("outputXML.php", function(data)
{
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++)
{
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<div class='phoneytext'> <b>" + name + "</b> <br/>" + address + "</div>";
var icon = customIcons[type] || {};
var marker = new google.maps.Marker
({
map: map,
position: point,
icon: icon.icon,
title:name
});
//Initiate an infoBubble - shows when a marker is tapped/clicked
infoBubble = new InfoBubble
({
map: map,
shadowStyle: 1,
padding: 0,
backgroundColor: 'rgb(57,57,57)',
borderRadius: 4,
arrowSize: 10,
borderWidth: 1,
borderColor: '#2c2c2c',
disableAutoPan: true,
arrowPosition: 30,
backgroundClassName: 'phoney',
arrowStyle: 2
});
bindInfoBubble(marker, map, infoBubble, html);
}
});
function bindInfoBubble(marker, map, infoBubble, html)
{
google.maps.event.addListener(marker, 'click', function()
{
infoBubble.setContent(html);
infoBubble.open(map, marker);
});
}
答案 0 :(得分:2)
您可以实施Singleton
概念:
downloadUrl("outputXML.php", function(data)
{
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++)
{
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<div class='phoneytext'> <b>" + name + "</b> <br/>" + address + "</div>";
var icon = customIcons[type] || {};
var marker = new google.maps.Marker
({
map: map,
position: point,
icon: icon.icon,
title:name
});
bindInfoBubble(marker, map, html);
}
});
var InfoBubbleClass = (function(){
var instance = null;
return {
getInstance:function(){
if(instance===null){
instance = new InfoBubble
({
map: map,
shadowStyle: 1,
padding: 0,
backgroundColor: 'rgb(57,57,57)',
borderRadius: 4,
arrowSize: 10,
borderWidth: 1,
borderColor: '#2c2c2c',
disableAutoPan: true,
arrowPosition: 30,
backgroundClassName: 'phoney',
arrowStyle: 2
});
}
return instance;
}
};
})();
function bindInfoBubble(marker, map, html)
{
google.maps.event.addListener(marker, 'click', function()
{
InfoBubbleClass.getInstance().setContent(html);
InfoBubbleClass.getInstance().open(map, marker);
});
}