我有一个 MySQL 表,每行都有纬度和经度坐标。按照这些说明,我创建了一个生成此 XML 代码的 php 文件:
<markers>
<marker name="Altopascio" anno="b. dal 1991 al 2000" gruppi="Sinti" affollamento="da 26 a 50" lat="10.6744775772094" lng="43.8144836425781" criticita="bassa" lat2="4852647" lng2="1634697"/>
<marker name="Buggiano" anno="a. fino al 1990" gruppi="Rom istriani" affollamento="da 51 a 100" lat="10.7345352172851" lng="43.8763999938964" criticita="nessuna" lat2="4859624" lng2="1639383"/>
<marker name="Carrara - Insediamento di Lavello" anno="a. fino al 1990" gruppi="Rom Ex Iugoslavia" affollamento="da 26 a 50" lat="10.1012363433837" lng="44.0793380737304" criticita="media" lat2="4881291" lng2="1588201"/>
[...]
</markers>
然后,仍然按照 Google Dev 的说明,我使用以下代码创建了地图:
<script type="text/javascript">
//<![CDATA[
var customIcons = {
nessuna: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
bassa: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
media: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
alta: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_yellow.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(43.7639427185058, 11.3140087127685),
zoom: 13,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("gisajax_genxml.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
//alert(markers.length);
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
//alert(i);
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>";
var icon = customIcons[criticita] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
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>
我不明白为什么我在地图上看不到标记图标。感谢您的帮助。