I have been developing a web service program that tracks the location of users using geolocation. The program initiates geolocation to locate the user and then proceeds to record the location. However, since geolocation relies on a callback function to retrieve the location, the program cannot proceed until it has obtained this information. This poses an issue as other variables are required for the program to continue within the geolocation function. To address this, I have configured the geolocation function to display the user's location on the webpage using the following function:
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
};
}
This function is triggered by the following code:
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);
});
The objective is to trigger the successful function once geolocation data is obtained, as a callback function. However, it seems that the program is skipping over most of the successful callback function and proceeding directly to the line dump(...). Any recommendations or insights on how I should properly write the successful function? Your input is greatly appreciated.