Hello. Can anyone explain why one cannot set a global variable to the values you get in the callback function from navigator.geolocation.getCurrentPosition( callbackFunction );
and how to go about using this values in the OpenWeather url in a more elegant way than nesting the function inside of the callback function for the navigator.geolocation
function?
You see most people’s local weaather projects I’ve seen have either used the ip address api to get the location or used the navigator.geolocation
as follows:
//Call the navigator.geolocation when document loads
$(document).ready(function() {
navigator.geolocation.getCurrentPosition( geolocationCallback );
});
//Write their callback function for the navigator.geolocation function
function geolocationCallback( position ){
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
//The call another function to process the weather by using getJSON( api_url , weatherAPIcallback );
getWeather( latitude, longitude );
}
I have seen this done in many solutions but there must be a more elegant/practical/organized way to go about it. Or is this good practice? Wouldn’t it be better to be able to manage all of this by beeing able to store the values as global variables and then writing independent callback functions for each API ( navigator.geolocation and open weather) ?
Anyone?