0

我做了一个简单的网站,上面有 javascript,它调用:

navigator.geolocation.getCurrentPosition(show_map, show_map_error);

我已经把网站放到了互联网上。我试图从不同位置的不同电脑(没有 GPS 小工具)打开网站。一个来自我家,一个来自朋友办公室。

但脚本并不总能获得位置。会有什么问题?

谢谢你。

4

1 回答 1

0

该方法不能保证返回位置,尤其是在没有连接 GPS 的情况下。

您可以尝试获取缓存位置。请参阅 API 规范中的以下内容

// Request a position. We only accept cached positions, no matter what 
// their age is. If the user agent does not have a cached position at
// all, it will immediately invoke the error callback.
navigator.geolocation.getCurrentPosition(successCallback,
                                         errorCallback,
                                         {maximumAge:Infinity, timeout:0});

function successCallback(position) {
  // By setting the 'maximumAge' to Infinity, the position
  // object is guaranteed to be a cached one.
  // By using a 'timeout' of 0 milliseconds, if there is
  // no cached position available at all, the user agent 
  // will immediately invoke the error callback with code
  // TIMEOUT and will not initiate a new position
  // acquisition process.
  if (position.timestamp < freshness_threshold && 
      position.coords.accuracy < accuracy_threshold) {
    // The position is relatively fresh and accurate.
  } else {
    // The position is quite old and/or inaccurate.
  }
}

function errorCallback(error) {
  switch(error.code) {
    case error.TIMEOUT:
      // Quick fallback when no cached position exists at all.
      doFallback();
      // Acquire a new position object.
      navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
      break;
    case ... // treat the other error cases.
  };
}

function doFallback() {
  // No cached position available at all.
  // Fallback to a default position.
}
于 2012-08-28T03:03:54.750 回答