【发布时间】:2017-04-10 14:54:43
【问题描述】:
我正在尝试从 MySQL 数据库中为我的标记(坐标等)获取一些数据,然后使用单击侦听器将这些标记添加到地图以打开 infoWindow。
这是我的代码:
// Request to database
function getLocations() {
if (window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
} else {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST", "getlocations.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var returnValue = JSON.parse(xmlhttp.responseText);
var infoWindow = new google.maps.InfoWindow;
for(var i=0; i<returnValue.length; i++) {
var marker = new google.maps.Marker({
map: map,
icon: icons['ice_green'].icon,
position: new google.maps.LatLng(returnValue[i][2], returnValue[i][3]),
title: '' + returnValue[i][1],
});
marker.addListener('click', function() {
infoWindow.setContent('test');
infoWindow.open(map, marker);
});
}
}
};
xmlhttp.send();
}
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 35.4681174, lng: 5.3043723},
zoom: 17
});
getLocations();
}
到目前为止一切正常。标记在正确的位置,但是当我单击标记中的一个时,“infoWindow”仅针对最后添加的标记打开。
有什么想法吗?问题出在哪里?
谢谢
【问题讨论】:
-
你只创建了一个
infoWindow把这行var infoWindow = new google.maps.InfoWindow;放在marker.addListener('click', function() {里面是我的猜测
标签: javascript mysql ajax google-maps