I am getting the user's location using the HTML5 Geolocation api like this:
navigator.geolocation.getCurrentPosition(function (position) {
loadWeather(position.coords.latitude + ',' + position.coords.longitude); //load weather using your lat/lng coordinates
});
jQuery(document).ready(function() {
loadWeather('Seattle',''); //@params location, woeid
});
function loadWeather(location, woeid) {
jQuery.simpleWeather({
location: location,
woeid: woeid,
unit: 'c',
success: function(weather) {
html = '<div class="weather_widget text-right"><h2><div class="icon_weather"><i class="icon-'+weather.code+'"></i></div><div class="degrees"><div class="temp_weather">'+weather.temp+'</div><div class="weather_deg">°</div><div class="unit_weather">'+weather.units.temp+'</div></div></h2>';
html += '<div class="city_weather">'+weather.city+'</div></div>';
jQuery("#weather").html(html);
},
error: function(error) {
jQuery("#weather").html('<p>'+error+'</p>');
}
});
}
This basically loads a widget that shows the weather based on the geolocation position.
Now my questions are:
How could I store the location variable for the entire session? (So that it does not ask for the location every time a new page is loaded?)
How can I make the location variable accessible to the entire website, so that I can use that location in other functionality on the website? (Such as a google map).
The geolocation code is located in a header file that is included across the site.