我正在尝试从 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>