我创建了一个简单的网站,其中包含javascript调用:
navigator.geolocation.getCurrentPosition(show_map,show_map_error);
我把网站放在网上。我试图在不同的位置从不同的PC(没有GPS小工具)打开网站。一个来自我家,一个来自朋友办公室。
但剧本并不总能得到一个位置。 会有什么问题?
谢谢。
答案 0 :(得分: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.
}