[英]Is there a way to check if geolocation has been DECLINED with Javascript?
[英]Using a callback once the geolocation has been found
我正在編寫一個程序來記錄用戶位置的網絡服務。 該程序使用地理位置來查找用戶,並且一旦檢索到位置,便會繼續記錄該位置。 因為地理位置使用回調函數來獲取位置,所以程序只有在找到位置后才能繼續。 問題是由於需要其他變量,我無法在地理定位功能內繼續執行該程序。 因此,我所做的是geoolcaion函數使用以下函數在網頁上顯示了位置
function geoFindMe(){
navigator.geolocation.getCurrentPosition(success, error, geo_options);
function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var altitude = position.coords.altitude;
var accuracy = position.coords.accuracy;
var time = getTime();
//post the everything somewhere and then the callback function will fetch the info
//may use var time = position.timestamp;
$("#result").append(time + " " + latitude + " " + longitude);
}
function error(error) {
alert("Unable to retrieve your location due to "+error.code + " : " + error.message);
}//there was a semicolon here
var geo_options = {
enableHighAccuracy: true,
maximumAge : 30000,
timeout : 27000
};
}
以下代碼調用此函數
geoFindMe(function successful(){
//first call geoFindMe function then call successful as a callback
//the function will retrieve the information that was displayed by the geoFindMe on the webpage
$("#result").hide();
var currentLocation = document.getElementById('result');
document.getElementById('result').innerHTML = "";
var moved = hasMoved(previousLocation, currentLocation);
var time = isItTime(moved, lastBuffer, 15);
if(time){//its time to put the location in the buffer
currentLocation = currentLocation + "#";// this way we will be able to differentiate between different locations
charBuffer.wrap(currentLocation);
}
dump(stringArray, lastDump, true);
});
我想做的是一旦找到地理位置,然后調用成功的函數,我試圖做一個回調函數。 問題是由於某種原因,它跳過了大多數成功的回調函數,並繼續執行dump(...)行; 有什么建議么? 也許我沒有正確編寫成功的函數? 非常感謝
您所要做的就是在geoFindMe函數中添加一個參數以接受回調。 然后在成功函數中調用它
function geoFindMe(callback){
navigator.geolocation.getCurrentPosition(success, error, geo_options);
function success(position) {
callback&&callback(); // Check if callback exists and then call it.
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var altitude = position.coords.altitude;
var accuracy = position.coords.accuracy;
var time = getTime();
//post the everything somewhere and then the callback function will fetch the info
//may use var time = position.timestamp;
$("#result").append(time + " " + latitude + " " + longitude);
}
function error(error) {
alert("Unable to retrieve your location due to "+error.code + " : " + error.message);
}//there was a semicolon here
var geo_options = {
enableHighAccuracy: true,
maximumAge : 30000,
timeout : 27000
};
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.