我有一個新的谷歌地圖API添加多個標記和顯示infowindow爲每個標記,所有我的標記顯示,但我的主要問題是infoWindow總是顯示在一個地方,我看了everywere,但似乎無法找到正確的解決方案,所有我要顯示的信息來自數據庫,所以我使用xmlhttp.responseText從另一個函數獲取信息是getData。谷歌API API信息窗口顯示在谷歌地圖API下1標記
我的代碼如下
function showMap(data){
//Changing the json respose back to the javascript array
var LongLat = eval('(' + data + ')');
// Creating a new map
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(-26.1981, 28.0488),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
for (var i = 0; i < LongLat.length; i++) {
latLng = new google.maps.LatLng(LongLat[i][0],LongLat[i][1]);
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position:latLng,
map: map,
title: LongLat[i][0]
});
var infoWindow = new google.maps.InfoWindow();
// Attaching a click event to the current marker
google.maps.event.addListener(marker, "click", function(point){
getData(this.position);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
infoWindow.setContent(xmlhttp.responseText);
infoWindow.open(map,marker);
}
}
});
}
}
和我的getData函數如下
function getData(d){
//將對象分隔爲經度和經度 p =(d.toString());
c = p.indexOf(',');
e = p.indexOf(')');
lat = p.substring(1,c);
lon = p.substring(c+1,e);
if(d == "results"){
var htmlCode = t;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('results').innerHTML = xmlhttp.responseText;
}
}
if(xmlhttp.readyState==4 && xmlhttp.status==200){
alert(xmlhttp.responseText);
return xmlhttp.responseText;
}
var html = xmlhttp.open("GET","mapmaker.php?lat="+lat+"&lon="+lon,true);
xmlhttp.send();
}
你是否嘗試尋找其他類似問題的答案?我有一種感覺,我已經多次看到這個問題... – slawekwin 2012-03-30 09:29:37