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));
  }
},

今のところ、システムがユーザーの位置を取得する前に JS 呼び出し _loadByUserLocation() を避けるために、setTimeout() 関数を使用する必要があります。

どうすれば修正できますか?

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

ユーザーの位置を検出するためにGoogle APIにアクセスする必要はありません。これはブラウザAPIです。

于 2013-07-20T17:55:51.953 に答える