这是 XML 的示例:
<markers><marker name="Faulkner State Community College" lat="30.853801" lng="-87.776692" type="PS" number="279" address="CFOT, CPCT" /></markers>
解析器正在获取名称、纬度、局域网和地址。它没有选择类型和号码。类型和数字的输出未定义。我不需要将它们转换为字符串以外的任何内容。
这是我的代码:
var markerNodes = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markerNodes.length; i++) {
var name = markerNodes[i].getAttribute("name");
var address = markerNodes[i].getAttribute("address");
var type = markerNodes[i].getAttribute("type");
var number = markerNodes[i].getAttribute("number");
var latlng = new google.maps.LatLng(
parseFloat(markerNodes[i].getAttribute("lat")),
parseFloat(markerNodes[i].getAttribute("lng")));
createMarker(latlng, name, address, type, number);
}
这是“createMarker”的代码,以防万一:
function createMarker(latlng, name, address, type, number) {
var html = "<font face=\"Arial\" size=\"2\"><b><a href=\"school.php?school_number=" + number + "\">" + name + "</a></b> <br/>Certifications: " + address + "<br/>School #: " + number + "</font>";
var marker = new google.maps.Marker({
map: map,
position: latlng,
icon: customIcon(type)
});
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
markers.push(marker);
}