我无法使用Google Maps API版本3在Google地图上绘制多个标记。我正确加载了地图,并且XML Feed也正常运行。我无法使用javascript来绘制标记。
这是我的脚本标记。 (主要取自Google教程)
<script type="text/javascript">
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 initialize() {
var mapOptions = {
center: new google.maps.LatLng(54.600035, -3.680419),
zoom: 6
};
var map = new google.maps.Map(document.getElementById("map-holder"), mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
downloadUrl("http://abcde.co.uk/xml-feed.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 address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
});
bindInfoWindow(marker, map, infoWindow, html);
}
});
</script>
我出错的任何想法?
XML响应看起来像这样......
<markers>
<marker name="name1" address="NW5 1JB" lat="51.560184" lng="-0.141326" type="Client" />
<marker name="name2" address="NG18 1QH" lat="53.146017" lng="-1.203534" type="Client" />
<marker name="name3" address="DN22 6NF" lat="53.321757" lng="-0.933399" type="Client" />
</markers>
答案 0 :(得分:1)
我认为您的变量map
是在initialize
函数内创建的。这意味着它的范围仅限于该函数:一旦它返回,map
“死亡”。
您需要对map
函数内的代码显示downloadUrl
。您可以将调用移至downloadUrl
函数内的initialize
,也可以提前声明地图。如果选择后者,请注意downloadUrl
函数完成之前initialize
上的回调不会发生。
答案 1 :(得分:0)
您的代码中存在许多javascript错误。当我修复它们(并使用稍微不同的downloadUrl版本)时,它对我有用。
<script type="text/javascript">
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(54.600035, -3.680419),
zoom: 6
};
var map = new google.maps.Map(document.getElementById("map-holder"), mapOptions);
var bounds = new google.maps.LatLngBounds();
var infoWindow = new google.maps.InfoWindow;
downloadUrl("SO_markers_20140309.xml", function(data) {
var xml = xmlParse(data);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
bounds.extend(point);
var html = "<b>" + name + "</b> <br/>" + address;
// var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
});
bindInfoWindow(marker, map, infoWindow, html);
}
map.fitBounds(bounds);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>