1

我正在尝试使用使用我编写的 PHP 文件生成的 XML 值生成具有多个标记的地图。我可以成功地将 PostGreSQL 数据库中的 XML 值打印到“markers.php”页面上。

但是,当我尝试将数据加载到 Google Maps 地图中时,地图本身不会显示或任何标记。放置地图的画布也不会出现。

谁能发现我的脚本有问题?PHP 方面的工作完美我只是在寻找一种解决方案,它将加载地图和具有从数据库中获取的 lat/lng 值的多个标记。请参阅下面的相关文件。

标记.php

session_start();
$username = $_SESSION['nickname']; 

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; 
}

$conn = pg_connect("dbconnectionstring");   

if(!conn){
    die ("Not connected : " . pg_last_error());
}

/*USE $username VARIABLE TO GET THE U_ID WHICH CORRESPONDS TO THE OWNER COLUMN IN RECORDS*/
$sql = "SELECT * FROM users WHERE username='$username'";
$sqlres = pg_query($sql);
$sqlassoc = pg_fetch_assoc($sqlres);
$owner = $sqlassoc['u_id'];

$query = "SELECT * FROM records WHERE owner='$owner'";
$result = pg_query($query);
if(!$result){
    die('Invalid query : ' . pg_last_error());
}

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

echo '<markers>';

while($row = pg_fetch_assoc($result)){

    echo '<marker ';
    echo 'name="' . parseToXML($row['name']) . '" '; 
    echo 'description="' . parseToXML($row['description']) . '" ';
    echo 'latitude="' .parseToXML($row['latitude']) . '" ';
    echo 'longitude="' .parseToXML($row['longitude']) . '" ';
    echo 'project="' .parseToXML($row['project']) . '" ';
    echo '/>';
}

echo '</markers>';

该脚本完美运行并适当地生成 XML。

Map.php - 包含 javascript

      function load(){
        var map = new google.maps.Map(document.getElementById("map"), {
         center: new google.maps.LatLng(51.4129, -4.0774),
        zoom: 13,
        mayTypeId: google.maps.MapTypeId.ROADMAP
    });
    var infoWindow = new google.maps.InfoWindow;

    /*markers.php file in same directory as map.php*/
    downloadUrl("markers.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("description");
        var project = markers[i].getAttribute("project");
        var point = new google.maps.LatLng(
            parseFloat(markers[i].getAttribute("latitude")),
            parseFloat(markers[i].getAttribute("longitude")));
        var html = "<b>" + name + "</b> <br />" + "<b>" + description + "</b> <br />" + project;
        var iconBase = 'http://users.aber.ac.uk/dwd/mfb/images';
        var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: iconBase + 'map_icon.png'
        });
        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.onreadstatechange = function() {
        if(request.readyState == 4) {
            request.onreadystatechange = doNothing;
            callback(request, request.status);
            }
        };

  request.open('GET', url, true);
  request.send(null);
}

function doNothing() {}

这段代码取自 Google Maps with PHP/MySQL 教程,但我看不到让它工作。有任何想法吗?

4

0 回答 0