JavaScript IE 中零星的地理位置
JavaScript geolocation sporadic in IE
我有一个与 this one 非常相似的问题,但我认为它在一个特别基本的方面有所不同,足以证明自己的问题。我有一个页面,使用 getCurrentPosition()
每 2 秒请求一次用户的位置(非常频繁,我知道,但这是有目的的)。这是我的代码:
function locationWrapper() {
if (!navigator.geolocation) {
success = false;
console.log("Geolocation is not supported by this browser")
}
if (success) {
navigator.geolocation.getCurrentPosition(
recordPosition, catchError, { enableHighAccuracy: false }
);
}
}
recordPosition()
函数并没有真正做任何与问题相关的事情(而且很少达到)。 catchError
看起来像这样:
function catchError(error) {
console.log(error);
switch (error.code) {
case error.PERMISSION_DENIED:
console.log("User denied the request for Geolocation.");
case error.POSITION_UNAVAILABLE:
console.log("Location information is unavailable.");
break;
case error.TIMEOUT:
console.log("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
console.log("An unknown error occurred.");
break;
}
}
一切都相当简单。这在 Chrome 中完美运行,在 IE 中仅 有时 。这就是莫名其妙的事情。它在 JS 文档的开头调用一次,然后由 setInterval()
函数每 2 秒调用一次。第一个 总是 有效,并且在间隔内大约每 20 次有效一次,但除此之外 returns 非常有用的 POSITION_UNAVAILABLE
错误。
我试过了:
将getCurrentPosition()
中的enableHighAccuracy
选项改成false
,在我的代码中可以看到
确保 IE 中的权限没有任何问题
将间隔时间延长至 5 秒
非常感谢任何意见。
编辑:
特别是IE11。抱歉错过了。
编辑 2:
我在使用 this example 时看到了相同的行为。第一次工作(显示坐标),但之后只显示错误,直到我重新加载。
所以这似乎是其他用户也遇到的 IE 问题。在 Edge 和 IE 支持页面上有一个关于它的 question,但是 OP 说 Edge 也没有工作(对我来说表现很好)并且似乎被忽略了,这有点混乱。我已经对该主题发表了评论,如果没有任何结果,我可能不得不提出我自己的仅与 IE 相关的问题。
对我来说似乎是一件大事 - 如果开发人员在需要时不能使用 getCurrentLocation()
,他们的网站将无法在 IE 中正常运行。我希望微软对此感兴趣。
我有一个与 this one 非常相似的问题,但我认为它在一个特别基本的方面有所不同,足以证明自己的问题。我有一个页面,使用 getCurrentPosition()
每 2 秒请求一次用户的位置(非常频繁,我知道,但这是有目的的)。这是我的代码:
function locationWrapper() {
if (!navigator.geolocation) {
success = false;
console.log("Geolocation is not supported by this browser")
}
if (success) {
navigator.geolocation.getCurrentPosition(
recordPosition, catchError, { enableHighAccuracy: false }
);
}
}
recordPosition()
函数并没有真正做任何与问题相关的事情(而且很少达到)。 catchError
看起来像这样:
function catchError(error) {
console.log(error);
switch (error.code) {
case error.PERMISSION_DENIED:
console.log("User denied the request for Geolocation.");
case error.POSITION_UNAVAILABLE:
console.log("Location information is unavailable.");
break;
case error.TIMEOUT:
console.log("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
console.log("An unknown error occurred.");
break;
}
}
一切都相当简单。这在 Chrome 中完美运行,在 IE 中仅 有时 。这就是莫名其妙的事情。它在 JS 文档的开头调用一次,然后由 setInterval()
函数每 2 秒调用一次。第一个 总是 有效,并且在间隔内大约每 20 次有效一次,但除此之外 returns 非常有用的 POSITION_UNAVAILABLE
错误。
我试过了:
将
getCurrentPosition()
中的enableHighAccuracy
选项改成false
,在我的代码中可以看到确保 IE 中的权限没有任何问题
将间隔时间延长至 5 秒
非常感谢任何意见。
编辑:
特别是IE11。抱歉错过了。
编辑 2:
我在使用 this example 时看到了相同的行为。第一次工作(显示坐标),但之后只显示错误,直到我重新加载。
所以这似乎是其他用户也遇到的 IE 问题。在 Edge 和 IE 支持页面上有一个关于它的 question,但是 OP 说 Edge 也没有工作(对我来说表现很好)并且似乎被忽略了,这有点混乱。我已经对该主题发表了评论,如果没有任何结果,我可能不得不提出我自己的仅与 IE 相关的问题。
对我来说似乎是一件大事 - 如果开发人员在需要时不能使用 getCurrentLocation()
,他们的网站将无法在 IE 中正常运行。我希望微软对此感兴趣。