Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何设置每10秒刷新位置的间隔?_Javascript_Html_Google Maps_Geolocation - Fatal编程技术网

Javascript 如何设置每10秒刷新位置的间隔?

Javascript 如何设置每10秒刷新位置的间隔?,javascript,html,google-maps,geolocation,Javascript,Html,Google Maps,Geolocation,我有这个代码,这是工作正常,我想刷新的位置每10秒,我搜索和尝试,但我无法找到的方式 var map, infoWindow; function initMap() { map = new google.maps.Map(document.getElementById('map'), { center: {lat: -34.397, lng: 150.644}, zoom: 6 }); infoWindow = new google.map

我有这个代码,这是工作正常,我想刷新的位置每10秒,我搜索和尝试,但我无法找到的方式

  var map, infoWindow;
  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: -34.397, lng: 150.644},
      zoom: 6
    });
    infoWindow = new google.maps.InfoWindow;

    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var pos = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        }

        infoWindow.setPosition(pos);
        infoWindow.setContent('Location found.');
        infoWindow.open(map);
        map.setCenter(pos);
        
我想我应该在这里设置一个间隔,但我不知道如何设置

      }, function() {
        handleLocationError(true, infoWindow, map.getCenter());
        interval
      });
    } else {
      // Browser doesn't support Geolocation
      handleLocationError(false, infoWindow, map.getCenter());
    }
  }

  function handleLocationError(browserHasGeolocation, infoWindow, pos) {
    infoWindow.setPosition(pos);
    infoWindow.setContent(browserHasGeolocation ?
   'Error: The Geolocation service failed.' :
   'Error: Your browser doesn't support geolocation.');
    infoWindow.open(map);
  }

我希望下面的解决方案能给你一点启发,谢谢

var映射,信息窗口; 函数初始化映射{ map=new google.maps.Mapdocument.getElementById'map'{ 中心:{ lat:-34.397, 液化天然气:150.644 }, 缩放:6 }; infoWindow=新建google.maps.infoWindow; getCurrentPositionmap,信息窗口 } 函数getCurrentPositionmap,信息窗口{ if navigator.geolocation{ navigator.geolocation.getCurrentPositionfunctionposition{ var pos={ 纬度:位置坐标纬度, lng:position.coords.longitude } infoWindow.setPositionpos; infoWindow.setContent“找到位置”; infoWindow.openmap; map.setCenterpos; setTimeoutgetCurrentPositionmap,信息窗口,10000; },功能{ handleLocationErrortrue,信息窗口,map.getCenter; 间隔 }; }否则{ //浏览器不支持地理位置 handleLocationErrorfalse,信息窗口,map.getCenter; } } 功能handleLocationErrorbrowserHasGeolocation、信息窗口、pos{ infoWindow.setPositionpos; infoWindow.setContentbrowserHasGeolocation? “错误:地理位置服务失败。”: 错误:您的浏览器不支持地理位置。; infoWindow.openmap;
}有什么理由每10秒更新一次吗?这将使您发送的请求超过所需数量=由于GMaps对API请求收费,因此成本更高。如果发生了变化,更新不是更有意义吗?我想用html5和sql制作一个应用程序,使用地图,我想发送当前位置,以防我需要在地图上画一个标记并实时看到其他标记。如果你是新手,那么我知道你的方法,这并不容易。但是,您应该稍后查看如何更新事件,而不是固定时间。它将提高性能并降低成本。您只需要了解如何保持和更新应用程序的状态。如果您正在监视位置的更改,为什么不使用watchPosition函数而不是getCurrentPosition?那你推荐什么呢?