Javascript和HTML5地理位置-如何存储位置
Javascript and HTML5 Geolocation - how to store the position?
我正在使用HTML5地理定位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>');
}
});
}
这基本上加载了一个小部件,它根据地理位置显示天气。
现在我的问题是:
如何存储整个会话的位置变量?(这样它就不会在每次加载新页面时询问位置了?)
如何使整个网站都可以访问位置变量,以便在网站的其他功能中使用该位置?(比如谷歌地图)。
地理位置代码位于整个站点中包含的头文件中。
Hi您需要使用javaScript 的localStorage
或sessionStorage
要在本地存储中存储数据,请使用以下代码
localStorage.setItem('veriable_name','value');
要获得价值,请使用以下代码
localStorage.getItem('veriable_name');
要清除本地存储,请使用
localStorage.clear();
有关本地存储和会话存储的更多信息
相关文章:
- Javascript 地理位置:将坐标存储在数组中
- Chrome/Tampermonkey 用户脚本存储在文件系统上的什么位置
- 如何在 Google App Engine 上存储 HTML 5 地理位置数据
- 在存储位置加载页面
- django-ckeditor:存储config.js的位置
- 创建存储按钮单击位置的数组
- 存储字母表中每个字母在所需位置的字符串中出现的次数
- 如何在文件中存储带有所需位置标记的地图
- 如何将标记位置保存到传单中的本地存储
- 我应该在 Laravel 5 中存储 JS 的什么位置
- 当用户不想被记住时存储令牌的位置
- 全局变量存储在 Node.JS 中的什么位置?节点中是否有类似窗口的对象
- 在 Redux 应用程序中写入本地存储的位置
- HTML5应用程序缓存文件存储在客户端的位置
- 浏览器如何存储用于自动完成的数据以及存储位置
- 如何找到firepad编辑器的文本存储位置以及如何清除编辑器
- Javascript和HTML5地理位置-如何存储位置
- Inc/dec逻辑遍历多个数组值并在数组中存储位置
- 检查多个本地存储位置
- React Components-HTML模板的存储位置