【发布时间】:2016-03-13 14:24:27
【问题描述】:
我正在使用 HTML5 Geolocation api 获取用户的位置,如下所示:
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>');
}
});
}
这基本上加载了一个基于地理位置位置显示天气的小部件。
现在我的问题是:
如何存储整个会话的位置变量? (这样它就不会在每次加载新页面时都询问位置?)
如何使整个网站都可以访问位置变量,以便在网站的其他功能中使用该位置? (例如谷歌地图)。
地理位置代码位于站点中包含的头文件中。
【问题讨论】:
-
您可以通过 cookie 或 sessionStorage 存储位置客户端。两者都可以通过 JavaScript 访问。
-
@SergeyBelyakov - 为什么你推荐 localStorage 而不是 sessionStorage?这是偏好问题还是有特定原因?
-
@musse1 感谢您的加急协助。
-
@Jacques ,sessionStorage(如名称一样)仅在浏览器会话期间可用(并且在窗口关闭时被删除)更多stackoverflow.com/questions/19867599/…
标签: javascript php jquery html geolocation