[英]navigator.gelocation still true on iOS when location services are off
Everything I've read says that in order to check if geolocation is available, to check for navigator.geolocation
.我读过的一切都说为了检查地理定位是否可用,检查navigator.geolocation
。 However, on iOS with location services turned off it still passes the check.但是,在关闭位置服务的iOS 上,它仍然通过检查。
It never reaches the else
.它永远不会到达else
。
Note: I am testing over https.注意:我正在通过 https 进行测试。
if(navigator.geolocation)
{
// on iOS safari, with location services off (globally or at the app level) this will log
console.log('navigator.geolocation passes');
// navigator.geolocation.getCurrentPosition does exist
console.log(navigator.geolocation.getCurrentPosition);
// attempt to make the call
navigator.geolocation.getCurrentPosition((position)=>
{
// this will not get called
console.log('successfully retrieved position')
},
(error)=>
{
// this will not get called
console.log('error: ', error)
})
}
else
{
// I would expect this to be called but it doesn't get called
console.log('geolocation unavailable')
}
Now, I'm not trying to get the location when location services are off, but the issue is that when they are off, it shouldn't pass the check.现在,我不想在位置服务关闭时获取位置,但问题是当它们关闭时,它不应该通过检查。
I suppose as a last resort, I could just set a variable to the coordinates and check if they're undefined or rather than rely on the block above, but if there's a better way to check for it then I'd like to do that.我想作为最后的手段,我可以只为坐标设置一个变量并检查它们是否未定义或不依赖于上面的块,但是如果有更好的方法来检查它,那么我想这样做.
Edit: I should also mention that this only occurs on the first load of the page after browser settings have been cleared (at least in my case).编辑:我还应该提到,这只发生在浏览器设置被清除后的第一次页面加载时(至少在我的情况下)。 On first load, it would pass the check and then hang because nothing else would be called.在第一次加载时,它会通过检查然后挂起,因为不会调用其他任何东西。 On second load it seemed to not pass the check and called our fallback option.在第二次加载时,它似乎没有通过检查并调用了我们的后备选项。
Edit: The
solution
is to set a variable outside of the check.编辑:
解决方案
是在检查之外设置一个变量。
// create a variable to hold the coordinates
let _coords = undefined;
// this is only a compatibility check
// not a check if it's available
if(navigator.geolocation)
{
// on iOS safari, with location services off (globally or at the app level)
// this block will be reached
// attempt to make the call to getCurrentPosition
navigator.geolocation.getCurrentPosition((position)=>
{
// this will not get called because location services are off
// doing something like doSomething(position.coords) will not get called
// instead, set the variable
_coords = position.coords;
},
(error)=>
{
// this will not get called since it's not an error
// doSomething(undefined);
})
}
else
{
// this block will not get reached since geolocation IS available,
// just not allowed. So again,
// doSomething(undefined) will not happen
console.log('geolocation unavailble')
}
// pass the variable (coords or undefined) to doSomething
doSomething(coords)
The above doesn't solve the entire issue since if the user does have locations services on, the getCoordinates is async so it will call the doSomething
method before it receives the coordinates.以上并没有解决整个问题,因为如果用户确实有位置服务,则 getCoordinates 是异步的,因此它将在接收坐标之前调用doSomething
方法。
I think this checking is for compatibility by testing for the presence of the geolocation object:我认为这种检查是通过测试地理定位对象的存在来实现兼容性的:
// check for Geolocation support
if (navigator.geolocation) {
console.log('Geolocation is supported!');
}
else {
console.log('Geolocation is not supported for this Browser/OS.');
}
I spoke too soon, and accepted too soon.我说得太早,也太快接受了。 In my example, moving the method outside of the geolocation check resulted in always "failing" the geolocation check because getCurrentPosition
is asynchronous.在我的示例中,将方法移到地理位置检查之外会导致地理位置检查总是“失败”,因为getCurrentPosition
是异步的。 The fix (could have sworn I tried this before) is to use a try/catch in the check.解决方法(可以发誓我以前尝试过)是在检查中使用 try/catch。
// geolocation
if(navigator.geolocation)
{
// try to get the current position
try {
navigator.geolocation.getCurrentPosition((position)=>
{
// great success! use the coordinates
doSomething(position.coords);
},
(err)=>
{
// failed
console.log(err);
// most likely due to a PositionError
// err.code === 1 (PERMISSION_DENIED)
doSomething();
})
} catch (err) {
// any other error
console.log(err);
}
}
else
{
// geolocation not supported
doSomething();
}
function doSomething(coords=undefined)
{
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.