我的Google Maps API的javascript出错了。我有一个几乎相同的脚本在另一个页面上运行,它运行完美但是当这个运行时,它写道:
"未捕获的TypeError:无法读取属性' documentElement' of null"
在控制台中。我只有一个documentElement引用,它与我在其他页面中正常工作的完全相同。我无法弄清楚为什么会抛出错误。
我的PHP制作XML:
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
$i = 1;
// Iterate through the rows, printing XML nodes for each
while ($row = @mysqli_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['firstName']) . '" ';
echo 'address="' . parseToXML($row['address1']) . '" ';
echo 'city="' . parseToXML($row['city']) . '" ';
echo 'zip="' . parseToXML($row['zip']) . '" ';
echo 'comments="' . parseToXML($row['comments']) . '" ';
echo 'location="' . parseToXML($row['location']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'distance="' . $i . '" ';
echo '/>';
$i++;
}
echo '<marker name="Test" address="123 Rose Road" city="Austin" zip="78745" comments="" location="" lat="" lng="" distance="home"/>';
// End XML file
echo '</markers>';
我的Google Maps API Javascript错误:
var customIcons = {
1: {
icon: 'mapicons/number_1.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(31.526342, -99.432643),
zoom: 10,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("phpsqlsearch_confirmed.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var city = markers[i].getAttribute("city");
var zip = markers[i].getAttribute("zip");
var distance = markers[i].getAttribute("distance");
var comments = markers[i].getAttribute("comments");
var location = markers[i].getAttribute("location");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + " - " + location + "</b> <br/>" + address + "<br/><font size=-1 color='blue'>" + comments + "</font>";
var icon = customIcons[distance] || {};
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() {}
//]]>
非常感谢任何帮助,如果您需要更多信息,请告诉我,因为我对javascript不太熟悉。
答案 0 :(得分:1)
问题是来自服务器的响应为空,请在chrome调试控制台上检查 - &gt;标签网络。检查php请求的响应,看看它是否为xml。