Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当位置服务关闭时,iOS上的navigator.gelocation仍然为真_Javascript_Ios_Geolocation - Fatal编程技术网

Javascript 当位置服务关闭时,iOS上的navigator.gelocation仍然为真

Javascript 当位置服务关闭时,iOS上的navigator.gelocation仍然为真,javascript,ios,geolocation,Javascript,Ios,Geolocation,我读到的所有东西都说,为了检查地理定位是否可用,检查navigator.geolocation。但是,在关闭位置服务的iOS上,它仍然通过检查 // 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 i

我读到的所有东西都说,为了检查地理定位是否可用,检查
navigator.geolocation
。但是,在关闭位置服务的iOS上,它仍然通过检查

// 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)
// 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)
{
  // ... 
}
它永远不会到达
其他

注意:我是通过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')
}
现在,我不想在位置服务关闭时获取位置,但问题是当它们关闭时,它不应该通过检查

// 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)
// 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)
{
  // ... 
}
我想作为最后的手段,我可以设置一个变量到坐标,检查它们是否未定义,而不是依赖上面的块,但是如果有更好的方法来检查它,那么我愿意这样做

编辑:我还应该提到,这只发生在浏览器设置被清除后(至少在我的情况下)第一次加载页面时。在第一次加载时,它将通过检查,然后挂起,因为不会调用任何其他内容。在第二次加载时,它似乎没有通过检查,并调用了我们的回退选项

编辑:解决方案是在检查之外设置一个变量

// 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)
// 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)
{
  // ... 
}

上述方法并不能解决整个问题,因为如果用户确实打开了locations服务,则getCoordinates是异步的,因此它将在收到坐标之前调用
doSomething
方法。

我认为这是通过测试geolocation对象的存在来检查兼容性的:

// check for Geolocation support
if (navigator.geolocation) {
  console.log('Geolocation is supported!');
}
else {
  console.log('Geolocation is not supported for this Browser/OS.');
}

我说得太快了,接受得太快了。在我的示例中,将方法移到地理位置检查之外会导致地理位置检查始终“失败”,因为
getCurrentPosition
是异步的。解决方法(我可以发誓我以前试过)是在支票中使用try/catch

// 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)
// 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)
{
  // ... 
}

这正是我们正在测试的情况;当定位服务关闭时。我已更新代码以包含else语句。它是通过https进行的。我已经对我的帖子做了一些更新,以便更清楚地表明,由于位置服务已关闭,我希望位置服务检查失败。不过,我非常感谢你的评论。谢谢:数据此代码只检查兼容性。这将检查设备是否与地理位置兼容。是的,这也是我确定的。至少对iOS来说是这样。我不能确定,但我们的测试人员似乎没有发现非iOS设备的问题。我还尝试在该检查中使用try/catch,但它从未抛出错误