谷歌MAPs api v2 AJAX无法正常工作

时间:2012-03-23 03:35:21

标签: google-maps

我的代码无法正常运行出错我无法理解......我的代码是:

/* =================Call.php========================*/
<?php
/* Include settings My mysql database */
include ("config.php");
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
     <title>GG-Tracker (GSM and GPS location combined)</title>
     <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=myAPIKey&sensor=true" type="text/javascript"></script>

<?php

echo "
   <script type=\"text/javascript\">
    var map;
   function load() {
     if (GBrowserIsCompatible()) {
       var map = new GMap2(document.getElementById(\"map\"));
   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 lat = parseFloat(markers[i].getAttribute(\"lat\"));
     var lon = parseFloat(markers[i].getAttribute(\"lon\"));
     var html = \"<b>\" +  \"</b> <br/>\" ;
     var marker = new GMarker(new GLatLng(lat, lon));
     map.addOverlay(marker);
     }
   }
 }
}
function downloadUrl(url, callback) {
   if (window.XMLHttpRequest)
     {
     request=new XMLHttpRequest();
     }
   else
     {
     request=new ActiveXObject(\"Microsoft.XMLHTTP\");
     }
  request.onreadystatechange = function() {
   if (request.readyState == 4) {
     request.onreadystatechange = doNothing();
     callback(request, request.status);
   }
  };
   request.open(\"GET\", url, true);
   request.send();
  }

 function doNothing() {}
  </script>
  ";
 ?>
 </head>
 <body onload="load()" onunload="GUnload()">
 <center>
  <div id="map" style="width: 800px; height: 600px"></div>
  </body>
  </html>
 /* End of =============Call.php===============*/

我的用于生成XML的phpsqlajax_genxml.php是:

 /* Start of ===========phpsqlajax_genxml.php=============== */
 <?php
 require("phpsqlajax_dbinfo.php");
 $dom = new DOMDocument("1.0");
 $node = $dom->createElement("markers");
 $parnode = $dom->appendChild($node);
 $connection=mysql_connect ($dbhost, $username, $password);
 if (!$connection) {
 die('Not connected : ' . mysql_error());
 }
 $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
$query = "SELECT * FROM markers";
$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
while ($row = @mysql_fetch_assoc($result)){
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("tid",$row['TID']);
$newnode->setAttribute("devid",$row['DevID']);
$newnode->setAttribute("ldate",$row['LDate']);
$newnode->setAttribute("ltime",$row['LTime']);
$newnode->setAttribute("lat", $row['Lat']);
$newnode->setAttribute("lon", $row['Lon']);
$newnode->setAttribute("speed", $row['Speed']);
}
echo $dom->saveXML();
?>
/*=========End of=============phpsqlajax_genxml.php*/

我的数据库信息文件是:phpsqlajax_dbinfo.php

/*=========Start of=== phpsqlajax_dbinfo.php============*/

<?php
$dbhost = "localhost";
$username="root";
$password="";
$database="mygps";
$gmaps = "AIzaSyCRf9drwSYjBSeKpvSkEHFKqX_yBpq-Tkk";
?>

/*===========End of phpsqlajax_dbinfo.php==============*/

以上三个文件无法正常工作。

如果我只运行phpsqlajax_genxml.php文件来生成XML,那么它正在运行 我可以生成XML,但无法通过JAVA下载 - 我认为AJAX调用不是 工作....

如何在没有的情况下在某个时间间隔内从MySQL自动重新加载标记 像Live Tracking .....重新加载整个页面。

请帮助我上面的代码无效

感谢阅读 普拉迪普

2 个答案:

答案 0 :(得分:0)

不推荐使用V2 API,您应该迁移到API的V3。

https://developers.google.com/maps/documentation/javascript/

答案 1 :(得分:0)

有没有理由使用php-echo输出HTML-source?

但是,如果上面的代码实际上是您实际使用的,那么您在此处遇到问题:

var markers = xml.documentElement.getElementsByTagName(\"marker
\");

换行符会破坏您的脚本,将其删除。