1

我正在开发一个网站,该网站将从我创建的 MySQL 数据库中读取latlng ,以便在 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('<','&lt;',$htmlStr); 
        $xmlStr=str_replace('>','&gt;',$xmlStr); 
        $xmlStr=str_replace('"','&quot;',$xmlStr); 
        $xmlStr=str_replace("'",'&#39;',$xmlStr); 
        $xmlStr=str_replace("&",'&amp;',$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");

我仍然无法理解如何解决此错误。有任何想法吗?

先感谢您。

4

2 回答 2

2
var infoWindow = new google.maps.InfoWindow;

应该

var infoWindow = new google.maps.InfoWindow();

同样在这里您通过“用户”(“用户”节点中的对象数组),但您肯定只想通过“用户”,即您刚刚创建的标记?

bindInfoWindow(users, map, infoWindow, html);
于 2012-06-01T10:43:45.530 回答
0

所以我想通了。在邓肯的回答之后,地标不起作用的原因是我没有放置

header("Content-type: text/xml");

genxml.php中。什么菜鸟,嗯?我认为邓肯的答案是正确的,因为没有它我就不会发现问题所在。下面是更新的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; 
    } 

    // Select all the rows in the users table
    $query = "SELECT * FROM users";
    $result = mysql_query($query);
    if (!$result) {
        die('Invalid query: ' . mysql_error());
    }

    header("Content-type: text/xml"); 

    // 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>';


?>
于 2012-06-01T18:06:37.840 回答