當第一次執行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