当第一次执行navigator.geolocation.getCurrentPosition(success, error, options);
时,我可以获取用户的位置。但是从第二次执行,该函数返回的错误:navigator.geolocation从第二次执行返回错误 - IE
The current position could not be determined.
我已按照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);
});
已回滚版本,因为我故意没有添加一段代码,它不反映错误,因此是没有用的。因此,小提琴,错误可以看到和测试。 – randomguy04