我有一个新的谷歌地图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