【问题标题】:Google Maps setTimeOut updating coordinates jquery javascript谷歌地图 setTimeOut 更新坐标 jquery javascript
【发布时间】:2013-10-16 13:26:02
【问题描述】:

因此,我尝试每五秒更新一次标记/用户位置,以便用户实时更新他们的位置。我可以让当前位置和坐标以正确的间隔更新,但是我希望谷歌地图上的标记更新,我不明白在谷歌地图代码中的哪里放置我的坐标以便实时更新?

    var x=document.getElementById("demo");

    function startGeolocation() {
      var options;
      navigator.geolocation.getCurrentPosition(geoSuccess, options);
      setTimeout(startGeolocation, 1000);
    }

    function geoSuccess(position) {
      var gpsPosition = position;
      var coordinates = gpsPosition.coords;
      myLat = coordinates.latitude;
      myLong = coordinates.longitude;
      //x.innerHTML="Latitude: " + myLat + 
      //"<br>Longitude: " + myLong;
      }

     var map;

     function initialize() {
     var mapOptions = {
     zoom: 18,
     mapTypeId: google.maps.MapTypeId.ROADMAP
     };
     map = new google.maps.Map(document.getElementById('map-canvas'),
     mapOptions);

   // Try HTML5 geolocation
   if(navigator.geolocation) {
   navigator.geolocation.getCurrentPosition(function(position, options) {
   //trying to pass in myLat and myLong from the live update above
   var pos = new google.maps.LatLng(position.coords.myLat,
                                   position.coords.myLong);

  var infowindow = new google.maps.InfoWindow({
    map: map,
    position: pos,
    content: 'You are here'
  });

  map.setCenter(pos);
  }, function() {
  handleNoGeolocation(true);
 });
 } else {
// Browser doesn't support Geolocation
 handleNoGeolocation(false);
 }
 }

 function handleNoGeolocation(errorFlag) {
 if (errorFlag) {
 var content = 'Error: The Geolocation service failed.';
 } else {
 var content = 'Error: Your browser doesn\'t support geolocation.';
 }

 var options = {
 map: map,
 position: new google.maps.LatLng(60, 105),
 content: content
 };

  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);
 }

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

【问题讨论】:

    标签: javascript jquery google-maps


    【解决方案1】:

    您使用了错误的功能。您应该使用 setInterval();

    setTimeout(表达式, 超时);超时后运行代码/函数一次。

    setInterval(表达式,超时);间隔运行代码/函数,它们之间的超时长度。

    https://stackoverflow.com/a/2696711/2332336

    【讨论】:

    • 谢谢,但这并不能解决我应该在 Google Maps Javascript 代码中输入坐标的问题,有人知道吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    • 2015-02-05
    • 2012-10-18
    • 1970-01-01
    相关资源
    最近更新 更多