我试图搜索这么多,但找不到适合我地图的任何解决方案!
我想建立一个网站,人们可以上传天气照片并在谷歌地图上显示。一切都很好,直到我想使用Geocoding!事实上,标记处于良好的位置,但信息窗总是相同的(最后一个标记信息)。我从一个从SQL数据库解析XML的php文件中获取地址(“name”)。 我知道很少有人遇到同样的问题,但即使有他们的解决方案,也不适合。
以下是代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>皆の天気byロマンスカイ</title>
<link rel="stylesheet" href="styletenki.css" />
<!--[if lte IE 9]>
<link rel="stylesheet" href="styleie.css" />
<![endif]-->
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder;
function load() {
geocoder = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 4,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("googleapitest.php", function (data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var date = markers[i].getAttribute("date");
//var point = new google.maps.LatLng(parseFloat(markers[i].getAttribute("lat")),
// parseFloat(markers[i].getAttribute("lgn")));
var type = markers[i].getAttribute("type");
var temperature = markers[i].getAttribute("temperature");
var cloud = markers[i].getAttribute("cloud");
var comment = markers[i].getAttribute("comment");
var html = "<b>" + name + "</b> <br/>" + temperature + "</b> <br/>" + cloud + "</b> <br/>" + comment;
//var icon = customIcons[type] || {};
/* Appel au service de geocodage avec l'adresse en parametre */
geocoder.geocode({
'address': name
}, function (results, status) {
/* Si l'adresse a pu etre geolocalisee */
if (status == google.maps.GeocoderStatus.OK) {
/* Affichage du marker */
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function () {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
request.onreadystatechange = function () {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
</script>
</head>
<body onLoad="load()">
<div id="map" style="width: 800px; height: 600px"></div>
</body>
</html>
我希望任何比我更好的谷歌地图API程序员都能找到解决方案!
答案 0 :(得分:0)
您的问题是在for循环期间html
中使用的bindInfoWindow(marker, map, infoWindow, html);
变量发生了变化。当调用geocoder.geocode
的回调时,这是用于所有标记的html
的最后一个值。要解决此问题,您必须使用闭包捕获html
的值。类似的东西:
(function(capturedHtml){
geocoder.geocode({
'address': name
}, function (results, status) {
/* Si l'adresse a pu etre geolocalisee */
if (status == google.maps.GeocoderStatus.OK) {
/* Affichage du marker */
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
bindInfoWindow(marker, map, infoWindow, capturedHtml);
}
});
})(html);