使用sql数据库更新谷歌地图标记

时间:2012-08-01 11:00:08

标签: javascript google-maps

我是谷歌地图的新手。我尝试使用谷歌地图开发跟踪系统。我成功开发了一个连接到SQL数据库的地图来检索标记。数据库根据gps单位更新。我想在10秒内在地图上显示一次。这是我的信息。如果有人可以,请帮助我。感谢。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps AJAX + mySQL/PHP Example</title>
 <script src="http://maps.google.com/maps/api/js?sensor=false"
        type="text/javascript"></script>

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

  var customIcons = {
  restaurant: {
    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(6.93360361159907, 79.85547582),
    zoom: 13,
    mapTypeId: 'roadmap'
  });
  var infoWindow = new google.maps.InfoWindow;

  // Change this depending on the name of your PHP file
  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 name = markers[i].getAttribute("name");
      var address = markers[i].getAttribute("address");
      var type = markers[i].getAttribute("type");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
      var html = "<b>" + name + "</b> <br/>" + address;
      var icon = customIcons[type] || {};
      var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon,
        shadow: icon.shadow
      });
      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 doNothing() {}

//]]>
</script>


</head>

 <body onload="load()">
 <div id="map" style="width: 1000px; height: 600px"></div>
 </body>
 </html>

1 个答案:

答案 0 :(得分:1)

如果您计划每10秒更新地图上的标记,则必须每10秒从数据库请求位置数据。对于小规模项目,polling数据库将是最简单的选项。否则,您可以查看web sockets

在回调中,您可以使用

更改标记位置
marker.setPosition()