Javascript GeoLocation缓存 [英] Javascript GeoLocation Caching

查看:56
本文介绍了Javascript GeoLocation缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法成功捕获用户的位置(移动浏览器):

I'm using the following to successfully capture user's location (mobile browser):

<script>
if ( navigator.geolocation ) {
navigator.geolocation.getCurrentPosition(handlePosition);
}
function handlePosition(pos) {
    //this passes lat/long to additional code
 }
</script>

这可行,但是通常浏览器似乎会缓存位置数据.调用此地理位置代码的页面显示了与用户位置有关的信息,因此发生的事情是用户可以移动(更改位置),重新加载页面,但是使用了先前的位置数据(显示了不正确的数据).有时,页面必须刷新一次或什至两次,页面才能使用新的位置数据.

This works, but often times the browser will seemingly cache the location data. The page that calls this geolocation code shows information relative to the user's location, so what happens is the user can move (change location), the page is reloaded, but the previous location data is used (showing incorrect data). Sometimes the page will have to be refreshed once or even twice for the page to use new location data.

有人知道每次脚本执行时强制获取并使用最新"位置数据的代码的任何方法吗?

Does anyone know of any means to force the code to get and use "up to date" location data each time script is executed?

FWIW,我在iOS Safari(6.1)中遇到问题.尚未能够在Android中进行测试.

FWIW, I'm experiencing problem in iOS Safari (6.1). Have not been able to test in Android yet.

感谢您的阅读和任何帮助.

Thanks for reading and for any help.

推荐答案

正如Oleksiy在他的回答中所写,Geolocation API现在支持此功能.您可以添加{maximumAge: 0}作为getCurrentPosition的第三个选项参数.如规范中所述,PositionOptions中还提供超时和高精度选项. .

As Oleksiy has written in his answer, the Geolocation API now supports this. You can add {maximumAge: 0} as the third option parameter of getCurrentPosition. There is also a timeout and a high accuracy option available in the PositionOptions as noted in the specification.

您的导航器调用将更改为以下内容:

Your navigator call would change to the following:

navigator.geolocation.getCurrentPosition(
  handlePosition, 
  (error)=>{}, 
  {maximumAge:0}
);

无法完成.除了示例中的代码之外,您对浏览器的地理位置没有任何控制权. html5地理位置api非常非常有限,这很痛苦.我也有一个问题是否可以问它如果已经授予了该域的许可,并且答案也是否".

No can't be done. You don't have any control over the browser geolocation other than the code in your example. The html5 geo location api is very, very limited and that is a pain. I also had a question whether I could ask it if permission for the domain had already been granted and the answer was also no.

问题在于该api是在浏览器本身中实现的,并且仅仅是这些功能的终结点.

The problem is that the api is implemented in the browser itself and that are just no endpoints for these kind of functions.

您可以做的是在js中创建一个数组来存储以前的位置,然后在对该数组更新视图测试以查看是否有陈旧的位置之前.

What you could do is make an array in js to store previous locations and before you update your view test against that array to see if you got a stale location.

这篇关于Javascript GeoLocation缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆