我得到“documentElement of null”错误。我正在努力跟随https://developers.google.com/maps/articles/phpsqlajax_v3 更新:在使用dev工具时,“downloadUrl()”函数似乎有问题。
我的“phpsqlajax\\u genxml.php”创建\\u元素(“标记”)$parnode=$doc->append\\u child($node);
// Opens a connection to a mySQL server
/*$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
die(\'Not connected : \' . mysql_error());
}
// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die (\'Can\\\'t use db : \' . mysql_error());
}*/
// Select all the rows in the markers table
global $wpdb ;
//$query = "SELECT * FROM markers WHERE 1";
$query = $wpdb->get_results("SELECT * FROM `markers` WHERE 1;") ;
/*no need of this
$result = mysql_query($query);
if (!$result) {
die(\'Invalid query: \' . mysql_error());
}*/
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
foreach ($query as $q)
{
// ADD TO XML DOCUMENT NODE
echo \'<marker \';
echo \'name="\' . parseToXML($q->name) . \'" \';
echo \'address="\' . parseToXML($q->address) . \'" \';
echo \'lat="\' . $q->lat . \'" \';
echo \'lng="\' . $q->lng . \'" \';
echo \'type="\' . $q->type . \'" \';
echo \'/>\';
}
?>
我的“hourly.php”文件从上面的文件中提取xml
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"
type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var customIcons = {
restaurant: {
icon: \'http://labs.google.com/ridefinder/images/mm_20_blue.png\'
},
bar: {
icon: \'http://labs.google.com/ridefinder/images/mm_20_red.png\'
}
};
function load() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 13,
mapTypeId: \'roadmap\'
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP file
downloadUrl("phpsqlajax_genxml.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 type = markers[i].getAttribute("type");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lat")),
parseFloat(markers[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon
});
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() {}
//]]>