我有一个很简单的JS使用navigator。geolocation。getcurrentposition jammy。
$(document).ready(function(){
$("#business-locate, #people-locate").click(function() {
navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
});
navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
function foundLocation(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var userLocation = lat + ', ' + lon;
$("#business-current-location, #people-current-location").remove();
$("#Near-Me")
.watermark("Current Location")
.after("<input type='hidden' name='business-current-location' id='business-current-location' value='"+userLocation+"' />");
$("#people-Near-Me")
.watermark("Current Location")
.after("<input type='hidden' name='people-current-location' id='people-current-location' value='"+userLocation+"' />");
}
function noLocation() {
$("#Near-Me").watermark("Could not find location");
$("#people-Near-Me").watermark("Could not find location");
}
})//end DocReady
基本上,这里发生的事情是我们得到当前位置,如果它得到了,两个“水印”被放置在两个字段中,表示“当前位置”,两个隐藏字段被创建,并使用latong数据作为它们的值(它们在开始时被删除,这样它们就不会每次都重复)。还有两个按钮,它们有一个点击功能,做同样的事情。
不幸的是,每隔三次左右,它就会奏效。
这里有什么问题?
这已经是一个老问题了,但所有的答案都不能解决我的问题,所以让我们加上我最终找到的那个。它闻起来像一种hack(它确实是一种),但在我的情况下总是有效。对你的处境也抱有希望。
//Dummy one, which will result in a working next statement.
navigator.geolocation.getCurrentPosition(function () {}, function () {}, {});
//The working next statement.
navigator.geolocation.getCurrentPosition(function (position) {
//Your code here
}, function (e) {
//Your error handling here
}, {
enableHighAccuracy: true
});
我也遇到了同样的情况。我尝试了超时解决方案,但不可靠。我发现如果你只调用它两次,位置就会正确刷新
function getLocation(callback)
{
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(position)
{
navigator.geolocation.getCurrentPosition(callback, function(){},{maximumAge:0, timeout:10000});
},function(){}, {maximumAge:0, timeout:10000});
}
return true;
}
这当然是有点慢,但我没有给我错误的位置一次。我有它击中超时几次,没有返回任何东西,但除此之外,它工作得很好。我知道这仍然有点老套,我期待有人找到真正的解决方案。
或者,如果你想确保它会一直尝试,直到你想放弃,你可以试试这样的方法。
//example
$(document).ready(function(){
getLocation(function(position){
//do something cool with position
console.log(position);
});
});
var GPSTimeout = 10; //init global var NOTE: I noticed that 10 gives me the quickest result but play around with this number to your own liking
//function to be called where you want the location with the callback(position)
function getLocation(callback)
{
if(navigator.geolocation)
{
var clickedTime = (new Date()).getTime(); //get the current time
GPSTimeout = 10; //reset the timeout just in case you call it more then once
ensurePosition(callback, clickedTime); //call recursive function to get position
}
return true;
}
//recursive position function
function ensurePosition(callback, timestamp)
{
if(GPSTimeout < 6000)//set at what point you want to just give up
{
//call the geolocation function
navigator.geolocation.getCurrentPosition(
function(position) //on success
{
//if the timestamp that is returned minus the time that was set when called is greater then 0 the position is up to date
if(position.timestamp - timestamp >= 0)
{
GPSTimeout = 10; //reset timeout just in case
callback(position); //call the callback function you created
}
else //the gps that was returned is not current and needs to be refreshed
{
GPSTimeout += GPSTimeout; //increase the timeout by itself n*2
ensurePosition(callback, timestamp); //call itself to refresh
}
},
function() //error: gps failed so we will try again
{
GPSTimeout += GPSTimeout; //increase the timeout by itself n*2
ensurePosition(callback, timestamp);//call itself to try again
},
{maximumAge:0, timeout:GPSTimeout}
)
}
}
我可能在这里有一些打字和拼写错误,但我希望你能明白。如果有人有问题或者找到更好的建议,请告诉我。
我在Mozilla遇到了这个问题。所有时间:错误:未知错误获取位置。
现在我使用47 Mozilla。我什么方法都试过了,但总是遇到这个问题。但是后来我在我的地址栏打开about:config,进入geo.wifi.ui,并将其值更改为“https://location.services.mozilla.com/v1/geolocate?key=test”。作品!
如果你有位置获取超时错误,尝试增加超时值:
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
navigator.geolocation.getCurrentPosition(success, error, options);
传递给geolocation . getcurrentposition()的第二个参数是要处理任何地理位置错误的函数。错误处理函数本身接收一个PositionError对象,其中包含关于地理定位尝试失败原因的详细信息。我建议输出错误到控制台,如果你有任何问题:
var positionOptions = { timeout: 10000 };
navigator.geolocation.getCurrentPosition(updateLocation, errorHandler, positionOptions);
function updateLocation(position) {
// The geolocation succeeded, and the position is available
}
function errorHandler(positionError) {
if (window.console) {
console.log(positionError);
}
}
Doing this in my code revealed the message "Network location provider at 'https://www.googleapis.com/' : Returned error code 400". Turns out Google Chrome uses the Google APIs to get a location on devices that don't have GPS built in (for example, most desktop computers). Google returns an approximate latitude/longitude based on the user's IP address. However, in developer builds of Chrome (such as Chromium on Ubuntu) there is no API access key included in the browser build. This causes the API request to fail silently. See Chromium Issue 179686: Geolocation giving 403 error for details.
大家也一样,顺便说一句,这在Chrome(稳定,开发和金丝雀)中工作完美,只是在FF和Safari中不合适。它在我的iPhone和iPad上也很好用(Safari!)这可能是由于这个功能相对较新(即它是一个bug)。我现在花了差不多一个星期的时间在这个上面,我就是不能让它在那些浏览器上工作
以下是我的发现:
第一次调用getCurrentPosition时,它工作得很完美。任何后续调用都不会返回,也就是说,它不会触发successCallback或errorCallback函数。为了证明我的观点,我在看涨期权中添加了几个仓位选项:
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {timeout: 10000});
并且每次都超时(在第一次成功调用之后)。我认为我可以用maximumAge来修复它,但这似乎并不像它应该工作的那样工作:
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {maximumAge:60000, timeout: 2000});
这应该防止实际调用getCurrentPosition函数,如果你在60秒内调用它,但它忽略了这一点(然而,这可能是因为我实际上刷新了我的页面来触发第二次调用,不确定这是否持久跨调用)
顺便说一句,即使谷歌的例子失败在这些浏览器上,这让我相信这确实是浏览器的bug,尝试一下,在Safari中加载两次,第二次就不会工作了。
如果有人找到了解决方案,请告诉我:-)
欢呼。