Google Map不会显示点 - XML为空 [英] Google Map won't display points - XML is null
问题描述
我正在开发一个网站,用于从我创建的MySQL数据库中读取 lat 和 lng 以在Google地图上显示它们。我使用这个 Google示例作为参考。从中读取lat和lng的表用户具有以下字段:
user_id
用户名
密码
名称
地址
lat
lng
国家
学院
电子邮件
电话
照片
角色
这是生成XML输出的 genxml.php 信息。
<?php
include(database.php);
function parseToXML($ htmlStr)
{
$ xmlStr = str_replace('<','& lt;',$ htmlStr);
$ xmlStr = str_replace('>','& gt;',$ xmlStr);
$ xmlStr = str_replace(''','& quot;',$ xmlStr);
$ xmlStr = str_replace(','&#39;',$ xmlStr);
$ xmlStr = str_replace(&,'& amp;',$ xmlStr);
return $ xmlStr;
}
//选择所有用户表中的行
$ query =SELECT * FROM users;
$ result = mysql_query($ query); $ b $ if(!$ result){
die('无效查询:'。mysql_error());
}
//启动XML文件,echo父节点
echo'< users>';
$ b ($ row = mysql_fetch_assoc($ result)){
//添加到XML文档节点
echo'< user ';
echo'name ='。 parseToXML($ row ['name'])。 ''';
echo'address ='。 parseToXML($ row ['address'])。 ''';
echo'lat ='。 $ row ['lat']。 ''';
echo'lng ='。 $ row ['lng']。 ''';
echo'/>';
}
//结束XML文件
echo'< / users>';
$ b?>
这是 admin.php ,它负责显示Google Map和标记。
<?php
session_start();
$ b $ if($ _ SESSION ['user_role']!=1){
header('Location:not_authorized.php');
}
?>
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 // ENhttp://www.w3.org/TR/html4/strict.dtd\">
< html xmlns =http://www.w3.org/1999/xhtmllang =elxml:lang =en>
<?php
include(database.php);
?>
< head>
< title> ARISTOTLE 2012 < / title>
< meta http-equiv =Content-Typecontent =text / html; charset = ISO-8859-7/>
< link rel =stylesheethref =/ styles / basic / input.csstype =text / cssmedia =screen/>
< link rel =快捷图标href =https://pithos.grnet.gr/pithos/rest/icsd08158@aegean.gr/files/favicon.ico/>
< ; meta name =viewportcontent =initial-scale = 1.0,user-scalable = no/>
< script src =http://maps.google.com/maps/api/js ?sensor = falsetype =text / javascript>< / script>
< script type =text / javascript>
var customIcons = {
2:{
图标:'http://labs.google.com/ridefinder/images/mm_20_blue.png',
shadow:'http://labs.google.com/ridefinder/images /mm_20_shadow.png'
},
栏:{
图标:'http://labs.google.com/ridefinder/images/mm_20_red.png',
shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
}
};
函数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();
//根据您的PHP文件的名称更改此值
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);
}
});
函数bindInfoWindow(user,map,infoWindow,html){
google.maps.event.addListener(user,'click',function(){
infoWindow.setContent(html);
infoWindow.open(map,user);
});
}
函数downloadUrl(url,callback){
var request = window.ActiveXObject?
new ActiveXObject('Microsoft.XMLHTTP'):
new XMLHttpRequest;
request.onreadystatechange = function(){
if(request.readyState == 4){
request.onreadystatechange = doNothing;
回调(request,request.status);
}
};
request.open('GET',url,true);
request.send(null);
}
函数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 =mapstyle =width:980px; height:400px>< / div>
< / body>
<?php
require_once(includes / footer.php);
?>
< / center>
< / td>
< / tr>
< / tbody>
< / table>
< br>
< br>
< / center>
< / body>
概念是,管理员可以在地图上看到注册用户。问题是标记不会显示。 Firebug给了我
$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ;用户名=Yale ... - 71.583336/>< / users>,response =< users>< user name =Yale ... - 71.583336/>< (第39行)
onreadystatechange()admin.php(第75行)
var users = xml.documentElement。的getElementsByTagName( 用户);
但我无法理解如何解决这个错误。有任何想法吗?
预先感谢您。
var infoWindow = new google.maps.InfoWindow;
应该是
var infoWindow = new google.maps.InfoWindow();
在这里,您还可以通过'users'('users'节点中的对象数组)但肯定你想通过'用户',即你刚刚创建的标记?
bindInfoWindow(用户,地图,infoWindow,html);
I'm developing a website that will read the lat and lng from the MySQL database that I've created to show them on Google Maps. I'm using this Google example as a reference. The table users from which I read the lat and lng has the following fields:
user_id
username
password
name
address
lat
lng
country
institute
email
phone
photo
role
This is the genxml.php that produces the XML output of the info.
<?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>';
?>
This is the admin.php that is responsible for showing Google Map and the markers.
<?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>
The concept is that admin can see on the map the registered users. The problem is that the markers won't display. Firebug gives me
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");
Still I cannot understand how to fix this error. Any ideas?
Thank you in advance.
var infoWindow = new google.maps.InfoWindow;
should be
var infoWindow = new google.maps.InfoWindow();
Also here you pass through 'users' (the array of objects in the 'users' node), but surely you want to pass through just 'user', i.e. the marker you've just created?
bindInfoWindow(users, map, infoWindow, html);
这篇关于Google Map不会显示点 - XML为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!