每x秒更新一次Google地图上的标记

正弦

我正在尝试根据AJAX调用返回的数据每隔x秒更新google map上的标记。每x秒调用一次Ajax函数,但未显示标记。以下是我编写的JavaScript。有人知道原因吗?谢谢。

    <script type="text/javascript">
  var map
  function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(1.32643, 103.79426),
      zoom: 11
    };
  map = new google.maps.Map(document.getElementById("map-canvas"),
        mapOptions);
  }


  google.maps.event.addDomListener(window, 'load', initialize);

        //Ajax call to get position
  function set_center() {
        var feedback = $.ajax({
            type: 'GET',
            url: 'get_gps_position',
          success: function (data) {
            console.log(data);
              if (data['gps_position_longitude'] != null && data['gps_position_latitude'] != null ) {
                var latlng = new google.maps.LatLng(data['gps_position_longitude'], data['gps_position_latitude']);
                var marker = new google.maps.Marker({
                position: latlng, 
                map: map,
                title:"Hello World!"});

              };


        },
        error: function(data) {
            $("#result").html(data);
            console.log(data)
        }
    });
  }

  setInterval(set_center, 10000);

</script>
莫勒博士

假设请求按预期运行,并返回有效的JSON:

var latlng = new google.maps.LatLng(data['gps_position_longitude'], data['gps_position_latitude']);

Agoogle.maps.LatLng期望参数按顺序排列latitude, longitude,而不是longitude,latitude

此外:最好不要创建单个标记并用于setPosition()更新位置,而不必在每个请求上都创建新的标记

另外:为了确保标记在视口内可见,还可以将地图中心设置为 latlng

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章