the*_*web 207 javascript jquery geolocation
所以我使用navigator.geolocation.getCurrentPosition jammy有一个相当简单的JS.
$(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
Run Code Online (Sandbox Code Playgroud)
基本上这里发生的是我们得到当前位置,如果得到的话,两个"水印"被放置在两个字段中,表示"当前位置",两个隐藏字段用lat-long数据作为它们的值(它们被删除)在一开始所以他们不会每次都重复).还有两个按钮,它们具有与之相关的点击功能,可以执行相同的操作.不幸的是,每三次左右,它都有效.这有什么问题???
bre*_*ung 328
我一直有完全相同的问题,几乎没有在网上找到有关它的信息.书中没有任何东西.最后,我在stackoverflow上找到了这个清醒的查询,并且(哈!)这是我在这里设置帐户所需的最终动力.
我有一个部分答案,但唉不完整.
首先,要意识到getCurrentPosition 的默认超时是无限的(!).这意味着如果getCurrentPosition挂在后端的某个地方,将永远不会调用您的错误处理程序.
为确保您获得超时,请将可选的第三个参数添加到对getCurrentPosition的调用中,例如,如果您希望用户在给他们了解发生的情况之前等待不超过10秒,请使用:
navigator.geolocation.getCurrentPosition(successCallback,errorCallback,{timeout:10000});
Run Code Online (Sandbox Code Playgroud)
其次,我在不同的环境中经历了完全不同的可靠性.在家里,我会在一两秒钟内收到回调,但准确性很差.
然而,在工作中,我经历了相当奇怪的行为变化:地理位置一直在某些计算机上运行(IE当然除外),其他只能用于chrome和safari而不是firefox(gecko问题?),其他工作一次,然后是失败 - 模式从一小时变为一小时,从一天到一天.有时候你有一台"幸运"的电脑,有时却没有.也许在满月时屠宰山羊会有帮助吗?
我无法理解这一点,但我怀疑后端基础设施比推广此功能的各种书籍和网站上的广告更不平衡.我真的希望他们能够更直接地了解这个功能的含义,以及超时设置的重要性,如果您希望错误处理程序正常工作.
我一直在努力向学生们传授这些东西,并且在我自己的计算机(在投影仪和几个大屏幕上)默默地失败的情况下,这种情况令人尴尬,而大约80%的学生几乎立即得到了结果(使用完全相同的无线网络).当我的学生也在制作拼写错误和其他失误时,以及当我自己的电脑也失败时,很难解决这些问题.
无论如何,我希望这有助于你们中的一些人.谢谢你的理智检查!
goi*_*ing 66
这是我解决这个问题的一种黑客方式,至少它适用于所有当前的浏览器(在Windows上,我没有Mac):
if (navigator.geolocation) {
var location_timeout = setTimeout("geolocFail()", 10000);
navigator.geolocation.getCurrentPosition(function(position) {
clearTimeout(location_timeout);
var lat = position.coords.latitude;
var lng = position.coords.longitude;
geocodeLatLng(lat, lng);
}, function(error) {
clearTimeout(location_timeout);
geolocFail();
});
} else {
// Fallback for no geolocation
geolocFail();
}
Run Code Online (Sandbox Code Playgroud)
如果有人点击关闭或选择否或在Firefox上选择"永不共享"选项,这也会有效.
笨重,但它的确有效.
小智 38
这对我来说每次都适用:
navigator.geolocation.getCurrentPosition(getCoor, errorCoor, {maximumAge:60000, timeout:5000, enableHighAccuracy:true});
Run Code Online (Sandbox Code Playgroud)
虽然不是很准确.有趣的是,如果我在同一台设备上运行,它会让我离开大约100米(每次),但如果我去google的地图,它会完全找到我的位置.因此,尽管我认为enableHighAccuracy:true有助于它始终如一地工作,但似乎并没有让它更准确......
Nie*_*eek 14
这已经是一个老问题,但所有答案都没有解决我的问题,所以让我们添加我最终找到的那个.它闻起来像一个黑客(它是一个),但总是在我的情况下工作.希望在你的情况下.
//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
});
Run Code Online (Sandbox Code Playgroud)
mvi*_*okx 13
同样在这里的人,这在Chrome(稳定版,开发版和金丝雀版)中运行完美,而不是FF和Safari.它也适用于我的iPhone和iPad(Safari!).这可能是由于此功能的相对新颖性(即它是一个错误).我现在花了将近一个星期的时间,我无法让它在这些浏览器上工作
这是我发现的:
The first time you call getCurrentPosition it works perfect. Any subsequent call never returns, i.e. it does not fire the successCallback or the errorCallback functions. I added a few position options to my call to prove my point:
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {timeout: 10000});
Run Code Online (Sandbox Code Playgroud)
and it times out every time (after the first successful call). I thought I could fix it with maximumAge, but that doesn't seem to be working as it is suppose to work either:
navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {maximumAge:60000, timeout: 2000});
Run Code Online (Sandbox Code Playgroud)
this should prevent actually calling the getCurrentPosition function if you call it within 60 seconds, but it ignores this (however, this could be due because I actually refresh my page to trigger the second call, not sure if this is persistent accross calls)
btw, even google's examples fail on these browsers which leads me to believe that this are indeed browser bugs, try it, load it twice in Safari and it won't work the second time.
If anybody finds a solution for this, PLEASE let me know :-)
Cheers.
我会把这个贴在这里,以防它对任何人有用……
在 iOS Safari 上,navigator.geolocation.getCurrentPosition
如果我有一个活动的navigator.geolocation.watchPosition
函数正在运行,调用会超时。
按照此处所述启动和停止watchPosition
正确使用clearWatch()
,有效:https : //developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition
您没有收到错误消息,因为它默认没有超时(至少我认为).我和firefox有同样的问题只有我firefox总是给出一个超时.您可以像这样自己设置超时.
我已将最大年龄设置为Infinity,以确保没有问题.我的功能在chrome中运行得很好,但我每次都在firefox中超时.
navigator.geolocation.getCurrentPosition(
function(position) {
//do succes handling
},
function errorCallback(error) {
//do error handling
},
{
maximumAge:Infinity,
timeout:5000
}
);
Run Code Online (Sandbox Code Playgroud)
我建议您仔细观察您的错误.应有尽有.为所有事情制定备份计划.我自己使用了数据库中的一些默认值或值,以防谷歌地理位置和导航地理位置失败.
截至 2020 年中期,这里的答案都没有提供任何解释,只是黑客攻击或猜测。
正如 @Coderer 在我之前指出的那样,如今需要安全上下文 (https),因此在越来越多的设备上,地理定位根本无法使用纯 http:
安全上下文 此功能仅在安全上下文 (HTTPS) 和部分或所有支持浏览器中可用。
第三个参数getCurrentPosition()
(watchPosition()
在这里更合适)是由以下属性组成的PositionOptions对象:
enableHighAccurancy
(默认 false):如果设置为 true,响应速度会更慢且更准确。如果出现超时错误,请将其保留为 false。如果精度较低,请将其设置为 true。在我的测试中,准确性有所不同,并且在某些设备上没有影响。在其他设备上设置为 true 可能会导致电池电量消耗得惊人地快。timeout
(默认无穷大):API 放弃并调用错误处理程序之前的毫秒数(第二个参数)。如今,大多数启用了地理定位并授予浏览器/页面权限的移动设备都会在一秒钟内以合理的精度获取该值。当地理定位服务不可用时,可以使用geo IP等替代方法,延迟可能会很多秒,并且精度通常从有问题到无用。maximumAge
(默认 0):缓存值有效时的毫秒数,设备可能决定使用有效的缓存数据而不是传感器测量。我通常使用缓慢移动的设备(例如带着手机行走)将其设置为非零值。在静态设备上,可以设置 Infinity 以避免随之而来的读取错误。正如 @YoLoCo 在我之前指出的那样,getCurrentPosition()
并且watchPosition()
干扰,我在 2020 年确认了他的结果。通常,使用 watchPosition 而不是 getCurrentPosition 定期调用。
这是我的解决方案,感谢闭包:
function geoloc(success, fail){
var is_echo = false;
if(navigator && navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function(pos) {
if (is_echo){ return; }
is_echo = true;
success(pos.coords.latitude,pos.coords.longitude);
},
function() {
if (is_echo){ return; }
is_echo = true;
fail();
}
);
} else {
fail();
}
}
function success(lat, lng){
alert(lat + " , " + lng);
}
function fail(){
alert("failed");
}
geoloc(success, fail);
Run Code Online (Sandbox Code Playgroud)
所以我遇到了同样的事情.我尝试了有效但不可靠的超时解决方案.我发现如果你只是叫它两次,那么这个位置会正确刷新
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;
}
Run Code Online (Sandbox Code Playgroud)
这当然有点慢,但我没有让它一次给我错误的位置.我已经让它超时了几次并且没有返回任何东西,但其他它然后效果很好.我知道这仍然有点hacky,我期待有人找到真正的解决方案.
或者如果你想确保它会继续尝试,直到你想放弃,你可以尝试这样的事情.
//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}
)
}
}
Run Code Online (Sandbox Code Playgroud)
我可能在这里有一些类型和一些拼写错误,但我希望你能得到这个想法.如果有人有疑问或有人找到更好的东西,请告诉我.
归档时间: |
|
查看次数: |
247636 次 |
最近记录: |