weixin_33749131 2018-05-04 10:22 采纳率: 0%
浏览 23

使用AJAX自动更新标记

I'm using this code to get gps data from (db.php from database), I need to update marker without refreshing the page. I tested setInterval (function ()), it works but it updates the whole page (map + marker). I need to update just the position of marker (variable uluru) but my problem is that the variable uluru is local

<html>
<head>
  <style>
      #map {
        height: 400px;
        width: 100%;
       }
    </style>
</head>
<body>
<div id="map"><b></b></div>
<div id="map2"><b>
  </b></div>

<script>


      var markers = [];
function loadmap(){
    
          var  xmlhttp = new XMLHttpRequest();
       
        xmlhttp.onreadystatechange = function () {
           if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
        var latt = myObj.name;
                var longg = myObj.name2;

                     uluru = {lat:+latt , lng:+longg };


map = new google.maps.Map(document.getElementById('map'), {
            zoom: 7,
            center:uluru,
          });
            
              addmarker(uluru);
                      

}
};
        xmlhttp.open("GET","db.php?q=",true);
        xmlhttp.send();
        
      }

                  function addmarker(location){
   var marker = new google.maps.Marker({ position: location, map: map });
                markers.push(marker);
              }
  
setInterval(function(){ loadmap() }, 1000);

</script>
<script
  async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD9J11XCcYDoT8-s0BD9sR4ARzIUmpI&callback=loadmap">
    </script>

</body>
</html>

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?