【问题标题】:navigator.geolocation returns error from second execution on - IEnavigator.geolocation 从第二次执行返回错误 - IE
【发布时间】:2018-02-02 21:14:50
【问题描述】:

第一次执行navigator.geolocation.getCurrentPosition(success, error, options); 时,我能够获取用户的位置。但是从第二次执行开始,函数返回错误:

当前位置无法确定。

我遵循了this question's answers 中的建议,但没有成功,我怎样才能让它发挥作用?

在这里您可以找到working fiddle 以快速查看错误。

//Pass this options to the getCurrentPosition
var options = {
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0
};
//function to execute if the current position was succesfully retrieved
function success(pos) {
console.log(pos);
  var crd = {lat: pos.coords.latitude, lng : pos.coords.longitude };
    var myPre = document.querySelector('pre');
  myPre.textContent = JSON.stringify(crd);
  myPre.style.color = someColor(); // use a diferent color just to see it's a new execution of the code
};
//execute this on error
function error(err) {
  var myPre = document.querySelector('pre');
  myPre.textContent = err;
  myPre.style.color = someColor(); // use a diferent color
};
//attach function to button
var myButton = document.querySelector('button');
myButton.addEventListener('click', function(){
    navigator.geolocation.getCurrentPosition(success, error, options);
});

【问题讨论】:

  • 回滚版本,因为我故意没有添加代码sn-p,它没有反映错误,因此没有用。因此小提琴,可以看到和测试错误。

标签: javascript internet-explorer geolocation


【解决方案1】:

Mouser 的解决方案在 IE11 中对我有用,但是会破坏 Edge,因此我们需要浏览器检测。这是我在 IE11、Edge 14、FFx 和 Chrome(撰写本文时最新版本的 FFx 和 Chrome)中测试的解决方案

            var currentPositionHasBeenDisplayed = false;

            if (navigator.geolocation) {

                var options = {};

                var isIE = document.documentMode; //IE 8+

                // IE only allows one call per script to navigator.geolocation.getCurrentPosition, so we need a workaround
                if (currentPositionHasBeenDisplayed == true && isIE) {
                    navigator.geolocation.watchPosition(
                         function (pos) {
                             var myLatLng = new google.maps.LatLng(parseFloat(pos.coords.latitude), parseFloat(pos.coords.longitude));
                             map.setCenter(myLatLng);
                         },
                         function (error) {  },
                         options);
                }
                else {
                    navigator.geolocation.getCurrentPosition(
                        function (pos) {
                            var myLatLng = new google.maps.LatLng(parseFloat(pos.coords.latitude), parseFloat(pos.coords.longitude));
                            map.setCenter(myLatLng);
                            currentPositionHasBeenDisplayed = true;
                        }, 
                        function (error) { return false; },
                        options);
                }
            }

【讨论】:

  • 我已经实现了相同但它破坏了 IE11,我也尝试在观看成功后清除手表位置.. :(
【解决方案2】:

我的想法如下:

IE用户只允许网站(脚本)(默认设置)运行getCurrentLocation一次。用户必须为其多次运行授予异常。

但是我不知道(也找不到任何文档)这种行为是设计使然还是错误。下面的解决方案是一种变通方法。

在最初成功绕过此错误后,请改用watchposition。查看更新的小提琴:https://jsfiddle.net/b2rnr7tw/6/

在这个小提琴中,我设置了一个watchPosition,一旦更新它就会显示新位置。之后它被取消(否则它会不断更新)。

//Pass this options to the getCurrentPosition
var options = {
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0
};

var watch = null;
var watchId = null;
//function to execute if the current position was succesfully retrieved
function success(pos) {

  var crd = {lat: pos.coords.latitude, lng : pos.coords.longitude };
  var myPre = document.querySelector('pre');
  myPre.textContent = JSON.stringify(crd);
  myPre.style.color = someColor(); // use a diferent color
  watch.clearWatch(watchId); //after success clear the watchId.
};
//execute this on error
function error(err) {
  var myPre = document.querySelector('pre');
  myPre.textContent = err;
  myPre.style.color = someColor(); // use a diferent color
  //keep running the watchPosition if on error, however you can use a counter to only try it a few times (recommended)
};
//attach function to button
var myButton = document.querySelector('button');
myButton.addEventListener('click', function(){
  if (!watch)
  {
    watch = navigator.geolocation;
    watch.getCurrentPosition(success, error, options);
   }
   else
   {
   watchId = watch.watchPosition(success, error, options);
   }
});

【讨论】:

  • 谢谢,我也找不到任何文档,你的方法有效,但是有一个奇怪的行为,从第二次执行开始,当我们开始执行 watchPosition 而不是 getCurrentPosition 时,它执行错误函数,然后成功函数,所以在视觉上你可以看到它抛出了一个错误,并且在用 Lat 和 Lng 值更新文本后不久,刚刚尝试了几台不同的计算机,你也明白了吗?我看到了函数的文档,但是发送参数的顺序很好
  • 我也得到了视觉行为,但这可以通过一些额外的代码轻松纠正。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
相关资源
最近更新 更多