实时更新标记

时间:2012-03-16 11:35:06

标签: javascript json google-maps

我需要从json文件中获取新位置,该位置将以regalr间隔更新,以便在地图上更新它,而无需重复重新加载整个页面。如何不使用Ajax

                 if (GBrowserIsCompatible()) {
                 //==add controls
                 var map = new GMap(document.getElementById("map"));
                 map.addControl(new GLargeMapControl());
                 map.addControl(new GMapTypeControl());
                 map.setCenter(new GLatLng(-29.870879, 30.977258),15);

                 var htmls = [];
                 var i = 0;

                 //create marker and set up infoWindow
                 function createMarker(point,ID,name) {
                 var marker = new GMarker(point);
                 GEvent.addListener(marker, "click", function() {
                 marker.openInfoWindowHtml(ID+"<br/>Name: " +name);

                 });

                  return marker;
                }



            process_Data = function(doc) {
                 //parse json file
                 var jsonData = eval('(' + doc + ')');

                 // ======== Plots the markers on Google Maps============

                   for (var i=0; i<jsonData.markers.length; i++) {
                       var point = new GLatLng(jsonData.markers[i].lat, jsonData.markers[i].lng);
                        var marker = createMarker(point,jsonData.markers[i].ID,jsonData.markers[i].name);
                       map.addOverlay(marker);

                       }
                     }

                      GDownloadUrl("points.json", process_Data);

                      }

1 个答案:

答案 0 :(得分:8)

var marker;

// every 10 seconds
setInterval(updateMarker,10000);

function updateMarker() {
   $.post('/path/to/server/getPosition',{}, function(json) {
      var LatLng = new google.maps.LatLng(json.latitude, json.longitude);
      marker.setPosition(LatLng);
   });
}