0

当我检测到用户的位置时,我试图调用一个函数,但我必须确保它使用 setTimeout() 函数的唯一方法。没有它我怎么能得到它?

_detectUserLocation: function(map, marker) {
  self = this;
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

      map.setCenter(pos);
      map.setZoom(12);
      marker.setPosition(pos);

      self._updateAddressPartsViaReverseGeocode(marker.getPosition());
      setTimeout(function() {
        self._loadByUserLocation(self.parsed);
      }, 2000);
    }, function(error) {
      marker.setPosition(new google.maps.LatLng(-23.5489433, -46.6388182));
    }, { timeout:5000 });
  } 
  else {
    marker.setPosition(new google.maps.LatLng(-23.5489433, -46.6388182));
  }
},

现在,我必须使用 setTimeout() 函数来避免在系统获得用户位置之前 JS 调用 _loadByUserLocation() 。

我该如何解决?

4

1 回答 1

0

GetCurrentPosition 有 3 个默认设置选项(成功、错误、选项),如评论中所述。查看示例和 MDN 站点的末尾。

navigator.geolocation.getCurrentPosition(success, error, options);

https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.geolocation.getCurrentPosition

你不访问谷歌 api 来检测你的用户位置,那是一个浏览器 api。

于 2013-07-20T17:55:51.953 回答