【发布时间】: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