0

我有一个基于 GPS 的系统,可以将坐标发送到 MYSQL 数据库。

使用此代码:

(function() {
    window.onload = function() {

        // Creating a new map
        var map = new google.maps.Map(document.getElementById("map"), {
          center: new google.maps.LatLng(41.65, -0.88),
          zoom: 1,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        function createPoints(json){
        var infoWindow = new google.maps.InfoWindow();

        // Looping through the JSON data
        for (var i = 0, length = json.locations.length; i < length; i++) {

                var data = json.locations[i],
                latLng = new google.maps.LatLng(data.lat, data.long);

            var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
            var marker = new google.maps.Marker({
                position: latLng,
                map: map,
                title: data.nome,
                icon: iconBase + 'schools_maps.png'
                });


            (function(marker, data) {

                // Attaching a click event to the current marker
                google.maps.event.addListener(marker, "click", function(e) {
                    infoWindow.setContent(data.nome);
                    infoWindow.open(map, marker);
                });


            })(marker, data);
                         /* moveMarker( map, marker ); */

         }

        }


        // Get  the JSON data from PHP script

var json ;

$.getJSON("http://mywebservice/nmea_last.php").done(function(data) {
   json = data;
    createPoints(json);
});

    }

})();

UdinggetJSON("http://mywebservice/nmea_last.php")句子,我得到了 gps 发送(周期性)到 mysql 的最后一个坐标,并且标记正确显示。我的问题是,我怎样才能获得标记的动态刷新以捕捉地图上的运动?

我想我需要使用 setTimeout 方法(或不需要?)但我不知道如何。有什么帮助吗?提前致谢。

4

3 回答 3

2

您可以使用 ajax pull 每 N 毫秒获取一次坐标:

var N = 1000; //every 1 second
var timeout = function() { 
  setTimeout(function()
  {
    $.getJSON("http://mywebservice/nmea_last.php").done(function(data) {
      json = data;
      createPoints(json);

      timeout(); //run again    
    }); 
  }, N); 
}

timeout();
于 2013-05-13T16:15:39.307 回答
1

尝试将 getJSON 调用更改为:

setTimeout( function() {
    $.getJSON("http://mywebservice/nmea_last.php").done(function(data) {
        json = data;
        createPoints(json);
    });
}, 5000);

5000 表示 5 秒延迟,您可以根据您希望屏幕刷新的时间进行调整。

于 2013-05-13T16:15:22.270 回答
1

我建议查看 setInterval 函数,您可以使用它定期调用函数。信息可以在这里找到。

我相信您会希望将 getJSON 调用包装在 setInterval 函数中以提取新点并刷新地图。这将是一个每 5 秒触发一次 getJSON 调用的示例:

setInterval(function() {
  $.getJSON("http://mywebservice/nmea_last.php").done(function(data) {
     json = data;
     createPoints(json);
  });
}, 5000);
于 2013-05-13T16:25:43.300 回答