admin管理员组

文章数量:1289565

I need fetch the new position from json file which will be updated at regualr intervals in order to update it on the map without reloading the whole page repeatedly. How can I do without using 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);

                      }

I need fetch the new position from json file which will be updated at regualr intervals in order to update it on the map without reloading the whole page repeatedly. How can I do without using 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);

                      }
Share Improve this question asked Mar 16, 2012 at 11:35 Fish123Fish123 251 gold badge2 silver badges6 bronze badges 3
  • Is there any reason why you don't want to use AJAX? – Dr.Molle Commented Mar 16, 2012 at 13:21
  • Not necessarily, if it works it will be perfect – Fish123 Commented Mar 16, 2012 at 13:36
  • You are using the Google Maps API V2, you should upgrade to V3 as V2 is deprecated and has little over a year of guaranteed life left. – Mano Marks Commented Mar 19, 2012 at 7:34
Add a ment  | 

1 Answer 1

Reset to default 9
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);
   });
}

本文标签: javascriptUpdate marker in real timeStack Overflow