我正在使用Xampp Apache Server
这个项目。基本上,该项目是将 CSV 转换为 XML 文档,XMLHttpRequest
用于循环遍历 XML 文档,提取每个位置的属性(例如:纬度、经度等),然后创建地图点标记。
下面是代码:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Read XML Files with Google Maps</title>
<script src="http://maps.google.com/maps? file=api&v=2&key=ABQIAAAA7_kD1t_m22HBF9feCaDPZxQwcATY4FXmxYwkk9LNWGtAQdNKTBS1kBsTEqrRPg2kWxuNdmf2JVCIkQ" type="text/javascript"></script>
<script src="markermanager.js"></script>
<script>
var map;
function initialize () {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(30.279, -97.700), 13);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.addMapType(G_PHYSICAL_MAP);
map.setMapType(G_PHYSICAL_MAP);
addMarkersFromXML();
}
}
function addMarkersFromXML(){
var batch = [];
mgr = new MarkerManager(map);
var request = GXmlHttp.create();
request.open('GET', 'Landmarks.xml', true);
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
var xmlDoc = request.responseXML;
var xmlrows = xmlDoc.documentElement.getElementsByTagName("Row");
for (var i = 0; i < xmlrows.length; i++) {
var xmlrow = xmlrows[i];
var xmlcellLongitude = xmlrow.getElementsByTagName("Longitude")[0];
var xmlcellLatitude = xmlrow.getElementsByTagName("Latitude")[0];
var point = new GLatLng(parseFloat(xmlcellLatitude.firstChild.data), parseFloat(xmlcellLongitude.firstChild.data));
//get the building name
var xmlcellBuildingName = xmlrow.getElementsByTagName("Building_Name")[0];
var celltextBuildingName = xmlcellBuildingName.firstChild.data;
//get the address
var xmlcellAddress = xmlrow.getElementsByTagName("Address")[0];
var celltextAddress = xmlcellAddress.firstChild.data;
//get the ownership
var xmlcellOwnership = xmlrow.getElementsByTagName("Ownership")[0];
var celltextOwnership = xmlcellOwnership.firstChild.data;
//get the date built
var xmlcellDateBuilt = xmlrow.getElementsByTagName("Date_Built")[0];
var celltextDateBuilt = xmlcellDateBuilt.firstChild.data;
var htmlString = "Building Name: " + celltextBuildingName + "<br>" + "Address: " + celltextAddress + "<br>" + "Ownership: " + celltextOwnership + "<br>" + "Date Built: " + celltextDateBuilt;
//var htmlString = 'yes'
var marker = createMarker(point,htmlString);
batch.push(marker);
}
mgr.addMarkers(batch,13);
mgr.refresh();
}
}
request.send(null);
}
function createMarker(point,html) {
var marker = new GMarker(point);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(html);
});
return marker;
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width: 1350px; height: 800px"></div>
<div id="message"></div>
</body>
</html>
和 XML 文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Row>
<Longitude>-97.772588</Longitude>
<Latitude>30.249405</Latitude>
<Building_Name>GOODRICH BAPTIST CHURCH AND CEMETERY</Building_Name>
<Address>2107 Goodrich Avenue</Address>
<Ownership>NON-PROFIT</Ownership>
<Date_Built>1860</Date_Built>
</Row>
<Row>
<Longitude>-97.744671</Longitude>
<Latitude>30.266270</Latitude>
<Building_Name>DAVIS (NELSON) WAREHOUSE</Building_Name>
<Address>107 West 4th Street</Address>
<Ownership>PRIVATE</Ownership>
<Date_Built>1905</Date_Built>
</Row>
</Root>
我有一个演示设置,但是当我尝试运行索引文件时,它不会在地图上显示标记。它会给我这个错误:
未捕获的类型错误:无法读取 null 的属性“documentElement”
谁能帮忙。赞赏。