我在添加多个标记并显示每个标记的infowindow时遇到了新的google maps api的问题,所有我的标记都显示但我的主要问题是infoWindow总是显示在一个地方,我看起来每个人都看到了但似乎无法找到正确的解决方案,我要显示的所有信息都来自数据库,因此我使用xmlhttp.responseText从另一个getData函数中获取信息。
我的代码如下
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){
//将对象分为lantitude和longtitude 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();
}
答案 0 :(得分:1)
我认为最好重用1个infoWindow。看看这个适用于我的代码片段:
var infowindow = new google.maps.InfoWindow();
function addToMap() {
for (var i = 0; i < marker_data.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(marker_data[i].lat, marker_data[i].lng),
clickable: true,
id:marker_data[i].id
});
google.maps.event.addListener(marker, "click", function() {
infowindow.close();
load_content(this, this.id);
});
marker.setMap(map);
/* here we keep track of the markers that we have added to the page */
markers_on_map.push(marker);
}
}
function load_content(marker, id){
$.ajax({
url: '/map/getMarkerWindow/' + id,
success: function(data){
infowindow.setContent(data);
infowindow.open(map, marker);
}
});
}
答案 1 :(得分:1)
你的代码几乎是好的:)只有一些错误:
将地图,infoWindow设为全局
var map = null;
var infoWindow = null;
function initialize()
{
// Fill w/ sample data
var LongLat = new Array();
LongLat[0] = new Array(-26.5, 28.5);
LongLat[1] = new Array(-26.0, 28.0);
// Creating a new map
map = new google.maps.Map(document.getElementById("map-canvas"), {
center: new google.maps.LatLng(-26.1981, 28.0488),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
infoWindow = new google.maps.InfoWindow();
for(var i = 0; i < LongLat.length; i++)
{
var latLng = new google.maps.LatLng(LongLat[i][0], LongLat[i][1]);
AddMarkerToMap(latLng, i);
} // END FOR ( LatLng)
}
将标记创建移动到单独的功能。这将隐藏变量
function AddMarkerToMap(latLng, i)
{
var marker = new google.maps.Marker({
position:latLng,
map:map
});
google.maps.event.addListener(marker, 'click', function()
{
infoWindow.setContent("Title: " + i);
infoWindow.open(map, marker);
});
}