我在谷歌地图 api v3 中使用 infoBubble 有问题。我从数据库加载标记并为该标记添加信息气泡。但是工作只是从最后添加的标记开始的信息气泡。你能看到问题吗?
var gmarkers = [];
var map;
function load() {
var defaultLocation = new google.maps.LatLng(49.8248188, 15.4684482);
var map = new google.maps.Map(document.getElementById("map"), {
center: defaultLocation,
zoom: 8,
mapTypeId: 'roadmap'
});
downloadUrl("generatexml.php", function (data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var id = markers[i].getAttribute("id");
var name = markers[i].getAttribute("name");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var marker = new google.maps.Marker({
map: map,
position: point
});
var infoBubble = new InfoBubble({
content: '<div' + name + '</div',
boxClass: 'info-box',
alignBottom: true,
pixelOffset: new google.maps.Size(-150, -40),
maxWidth: 300,
disableAutoPan: false,
hideCloseButton: false,
});
google.maps.event.addListener(marker, 'click', function () {
infoBubble.open(map, marker);
console.log(infoBubble.content);
});
gmarkers.push(marker);
}
});
}
我找到了它,只需替换:
google.maps.event.addListener(marker, 'click', function () {
infoBubble.open(map, marker); console.log(infoBubble.content); });
跟:
google.maps.event.addListener(marker, 'click', function () {
infoBubble.setContent(this.content); infoBubble.open(map, this); });
谢谢你的帮助。