我正在开发一个网站,该网站将从我创建的 MySQL 数据库中读取lat和lng ,以便在 Google 地图上显示它们。我使用这个Google 示例作为参考。我从中读取 lat 和 lng 的表 users 具有以下字段:
user_id
username
password
name
address
lat
lng
country
institute
email
phone
photo
role
这是生成信息的 XML 输出的genxml.php 。
<?php
include("database.php");
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Select all the rows in the users table
$query = "SELECT * FROM users";
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// Start XML file, echo parent node
echo '<users>';
// Iterate through the rows, printing XML nodes for each
while ($row = mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
echo '<user ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo '/>';
}
// End XML file
echo '</users>';
?>
这是负责显示谷歌地图和标记的admin.php 。
<?php
session_start();
if($_SESSION['user_role'] != "1"){
header( 'Location: not_authorized.php' ) ;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="el" xml:lang="en">
<?php
include ("database.php");
?>
<head>
<title> ARISTOTLE 2012 </title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-7" />
<link rel="stylesheet" href="/styles/basic/input.css" type="text/css" media="screen"/>
<link rel="shortcut icon" href="https://pithos.grnet.gr/pithos/rest/icsd08158@aegean.gr/files/favicon.ico" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">
var customIcons = {
2: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
},
bar: {
icon: 'http://labs.google.com/ridefinder/images/mm_20_red.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(42.357437, -71.096962),
zoom: 13,
mapTypeId: 'roadmap'
});
var infoWindow = new google.maps.InfoWindow();
// Change this depending on the name of your PHP file
downloadUrl("genxml.php", function(data) {
var xml = data.responseXML;
var users = xml.documentElement.getElementsByTagName("user");
for (var i = 0; i < users.length; i++) {
var name = users[i].getAttribute("name");
var address = users[i].getAttribute("address");
var type = users[i].getAttribute("role");
var point = new google.maps.LatLng(
parseFloat(users[i].getAttribute("lat")),
parseFloat(users[i].getAttribute("lng")));
var html = "<b>" + name + "</b> <br/>" + address;
var icon = customIcons[type] || {};
var user = new google.maps.Marker({
map: map,
position: point,
icon: icon.icon,
shadow: icon.shadow
});
bindInfoWindow(user, map, infoWindow, html);
}
});
}
function bindInfoWindow(user, map, infoWindow, html) {
google.maps.event.addListener(user, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, user);
});
}
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>
</head>
<body>
<center>
<table class="wrapper">
<tbody>
<tr>
<td valign="top">
<center>
<?php
require_once("includes/header.php");
?>
<?php
require_once("includes/admin_menu.php");
?>
<body onload="load()">
<div id="map" style="width: 980px; height: 400px"></div>
</body>
<?php
require_once("includes/footer.php");
?>
</center>
</td>
</tr>
</tbody>
</table>
<br>
<br>
</center>
</body>
这个概念是管理员可以在地图上看到注册用户。问题是标记不会显示。萤火虫给了我
xml is null
(?)(data=XMLHttpRequest { responseText="<users><user name="Yale..."-71.583336" /></users>", response="<users><user name="Yale..."-71.583336" /></users>", status=200, more...})admin.php (line 39)
onreadystatechange()admin.php (line 75)
var users = xml.documentElement.getElementsByTagName("user");
我仍然无法理解如何解决此错误。有任何想法吗?
先感谢您。