【问题标题】:Javascript and HTML5 Geolocation - how to store the position?Javascript 和 HTML5 地理位置 - 如何存储位置?
【发布时间】: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">&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>');
  }
  });
}

这基本上加载了一个基于地理位置位置显示天气的小部件。

现在我的问题是:

  1. 如何存储整个会话的位置变量? (这样它就不会在每次加载新页面时都询问位置?)

  2. 如何使整个网站都可以访问位置变量,以便在网站的其他功能中使用该位置? (例如谷歌地图)。

地理位置代码位于站点中包含的头文件中。

【问题讨论】:

  • 您可以通过 cookie 或 sessionStorage 存储位置客户端。两者都可以通过 JavaScript 访问。
  • @SergeyBelyakov - 为什么你推荐 localStorage 而不是 sessionStorage?这是偏好问题还是有特定原因?
  • @musse1 感谢您的加急协助。
  • @Jacques ,sessionStorage(如名称一样)仅在浏览器会话期间可用(并且在窗口关闭时被删除)更多stackoverflow.com/questions/19867599/…

标签: javascript php jquery html geolocation


【解决方案1】:

您好,您需要使用 localStoragesessionStorage 的 javaScript

要将数据存储在本地存储中,请使用以下代码

localStorage.setItem('veriable_name','value');

为了获得价值使用下面的代码

localStorage.getItem('veriable_name');

清除localStorage使用

localStorage.clear();

更多关于Local Storagesession Storage的信息

【讨论】:

    猜你喜欢
    • 2012-04-03
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2013-01-26
    • 1970-01-01
    • 2011-12-11
    相关资源
    最近更新 更多