对代码进行了更改,将依赖数据放入函数中,该函数向信息窗口返回正确的值,而不是 UNDEFINED。
问题来了,只打印了一个标记。
var geocoder;
var map;
function load() {
geocoder = new google.maps.Geocoder();
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(57.699322, 11.951093),
zoom: 6,
minZoom: 6,
maxZoom: 18,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// Get the xml data
downloadUrl("functions.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var point = new google.maps.LatLng(markers[i].getAttribute("lat"),markers[i].getAttribute("lng"));
codeLatLng(point, function(addr){
var name = markers[i].getAttribute("name").replace("_"," ");
var html = '<div class="markTitle">' + name.replace("_"," ") + '</div>'
+ '<div class="markAddress">'+addr+'</div>'
+ '<a class="markLink" href="http://www.wikiplace.se/index.php?title='+ name +'">'
+ 'Läs mer om '+ name.replace("_"," ") + ' här...</a>';
var marker = new google.maps.Marker({
map: map,
position: point,
uid: "id"+i});
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);});
}); // end codeLatLng
} // end for-loop
});// end downloadURL
}
这段代码似乎有效。
function codeLatLng(point, callback) {
geocoder.geocode({'latLng': point}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
callback(results[1].formatted_address);
} else {
alert("Couldn't find address");
}
} else {
alert('Geocoder failed due to: ' + status);
}
});
}
我还检查了 XML 文件是否有错误,但它的格式是正确的。有人对此有解决方案吗?
谢谢