2010-03-13 86 views
2

我是googlemaps API的新手。我为我的手机写了一个小应用程序,它定期将其位置更新为SQL数据库。使用php/mysql在Google地图上动态绘制多段线

我想在我的浏览器的googlemap上显示此信息。理想情况下,我想定期轮询数据库,如果有新的合作伙伴到达,请将它们添加到该行。

最佳描述它的方式是this

在寻求到那里,我已经开始在谷歌上的文件和被修改,以尝试和acheive我想要的。它不起作用 - 我不知道为什么。我想为什么喜欢一些建议,并且对我的最终目标的任何指示都会非常受欢迎。 谷歌地图AJAX + MySQL的/ PHP实例

<script type="text/javascript"> 
    //<![CDATA[ 

function load() { 
    if (GBrowserIsCompatible()) { 
    var map = new GMap2(document.getElementById("map")); 
    map.addControl(new GSmallMapControl()); 
    map.addControl(new GMapTypeControl()); 
    map.setCenter(new GLatLng(47.614495, -122.341861), 13); 

    GDownloadUrl("phpsqlajax_genxml.php", function(data) { 
     var xml = GXml.parse(data); 
     var line = []; 
     var markers = xml.documentElement.getElementsByTagName("points"); 
     for (var i = 0; i < points.length; i++) { 
      var point = points.item(i); 
      var lat = point.getAttribute("lat"); 
      var lng = point.getAttribute("lng"); 

      var latlng = new GLatLng(lat, lng); 

     line.push(latlng); 
     if (point.firstChild) { 
     var station = point.firstChild.nodeValue; 
     var marker = createMarker(latlng, station); 
     map.addOverlay(marker); 
     } 
    } 

    var polyline = new GPolyline(line, "#ff0000", 3, 1); 
    map.addOverlay(polyline); 
}); 
} 
//]]> 

我的PHP文件中生成以下XML;

<?xml version="1.0" encoding="UTF-8" ?> 
<points> 
<point lng="-122.340141" lat="47.608940"/> 
<point lng="-122.344391" lat="47.613590"/> 
<point lng="-122.356445" lat="47.624561"/> 
<point lng="-122.337654" lat="47.606365"/> 
<point lng="-122.345673" lat="47.612823"/> 
<point lng="-122.340363" lat="47.605961"/> 
<point lng="-122.345467" lat="47.613976"/> 
<point lng="-122.326584" lat="47.617214"/> 
<point lng="-122.342834" lat="47.610126"/> 
</points> 

我已经成功地完成了这个工作;尝试自定义代码之前,请尝试使用http://code.google.com/apis/maps/articles/phpsqlajax.html

任何指针?我哪里出错了?

回答

1

你似乎是在正确的轨道上。

你的PHP脚本应该接受一个时间戳参数,应检查是否有新的点都在戳后的数据库中插入。如果是,则应返回最新条目的响应(或者在该时间戳之后的条目列表,如果您希望在车辆移动时显示实时踪迹)。

在客户端,您可能需要使用普通或long polling以及上次更新的timestamp参数向服务器端脚本发起AJAX请求。

当你的AJAX请求从服务器接收新的信息,您只需在地图上移动的标记。然后用更新的时间戳参数启动一个新的AJAX请求。

伪上下的使用jQuery例如:

var lastUpdate = '2000/01/01 00:00:00'; 

function autoUpdate() { 
    $.ajax({ 
     type: "GET", 
     url: "phpsqlajax_genxml.php?last_update=" + lastUpdate, 
     dataType: 'xml', 
     success: function(xmlData) { 

      // 1. Check if the xmlData is empty. If not we received 
      // some fresh data. 
      // 2. Update lastUpdate from the xmlData with the timestamp from 
      // the server. Don't use JavaScript to update the timestamp, 
      // because the time on the client and on the server will 
      // never be exactly in sync. 
      // 3. Move the markers on Google Map. 

      // Relaunch the autoUpdate() function in 5 seconds. 
      setTimeout(autoUpdate, 5000); 
     } 
    }); 
} 
+0

感谢您的回复。我现在已经有了上面的代码,我犯了一些粗心的错误,我只是看不到。 AJAX对我来说是另一个新东西(不是网站上的tbh),但会考虑它并尝试应用你所说的。 – Mark 2010-03-13 22:21:05

相关问题