我正试图从MySql数据库加载标记(我使用了一个php文件,该文件转换了提供xml输出的数据),但我只看到了映射。在阅读了StackOverflow上的帖子后,我仍然不知道如何让它发挥作用。
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=true"></script>
</head>
<body>
<script>
function initialize()
{
var map=new google.maps.Map(document.getElementById("mapka"), {
center:new google.maps.LatLng(51.109612,17.036276),
zoom:12,
mapTypeId:google.maps.MapTypeId.ROADMAP
});
downloadUrl("dane.php", function(data)
{
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++)
{
var nazwa = markers[i].getAttribute("nazwa");
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new google.maps.LatLng(lat,lng);
var marker = new google.maps.Marker({
map: map,
position: point
});
}
});
}
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() {}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<div id="mapka" style="width:700px;height:500px;"></div>
</body>
</html>
XML如下所示:
<dane>
<marker lat="51.110287" lon="17.062647" nazwa="Hello_1" address="Street_1"/>
<marker lat="51.111807" lon="17.058012" nazwa="Hello_2" address="Street_2"/>
</dane>
您试图访问属性lng
,但为XML内部的标记定义的属性是lon