存储 HTML5 地理位置数据

2023-04-20前端开发问题
3

本文介绍了存储 HTML5 地理位置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

如何在 Rails 3 中存储和处理网站用户的地理位置(经纬度),以便它检查我们是否已经在每个页面请求的用户会话中保存了这些详细信息(如果我们没有保存详细信息,那么我们应该从浏览器请求用户的位置,然后将这些详细信息存储在会话中)?

How can I store and process the geolocation (long and lat) of a website user in Rails 3, so that it checks to see if we're already holding those details in a session for that user on every page request (if we're not holding the details, then we should request the user's location from the browser and then store those details in the session)?

推荐答案

根据您的要求,我想说您实际上并不需要 ajax,因为大部分处理将使用 JS 完成(询问用户访问他们的位置,解析响应等),我会使用 JS 设置一个 cookie,Rails 会看到).

Based on your requirements I'd say that you don't actually need ajax, since most of the processing will be done using JS (to ask the user for access to their location, parse the response etc), I'd use JS to set a cookie, which Rails will then see).

在您的控制器中

def action
  @lat_lng = cookies[:lat_lng].split("|")
end

在你看来

<%- unless @lat_lng %>
<script>
  getGeoLocation();
</script>
<%- end %>

在您的一个 javascript 文件中

function getGeoLocation() {
  navigator.geolocation.getCurrentPosition(setGeoCookie);
}

function setGeoCookie(position) {
  var cookie_val = position.coords.latitude + "|" + position.coords.longitude;
  document.cookie = "lat_lng=" + escape(cookie_val);
}

请注意,上述测试都不是为了查看用户是否拥有支持地理定位的浏览器,或者用户是否已授予(或拒绝)使用其位置的权限,并且该 cookie 将是一个会话 cookie,并且JS 不会测试是否已经设置了 cookie.要在 cookie 上设置更复杂的信息,请查看 http://www.quirksmode.org/js/cookies.html 有关使用 javascript 的 GeoLocation 的更多信息,请参阅 http://diveintohtml5.info/geolocation.html

Note that none of the above tests to see if the user has a browser that supports geolocation, or if the user has granted (or denied) permission to use their location, and that the cookie will be a session cookie, and that the JS doesn't test to see if the cookie is already set. To set more complicated information on the cookie take a look at http://www.quirksmode.org/js/cookies.html For more information on GeoLocation using javascript see http://diveintohtml5.info/geolocation.html

这篇关于存储 HTML5 地理位置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

layui 单选框、复选框、下拉菜单不显示问题如何解决?
1. 如果是ajax嵌套了 页面, 请确保 只有最外层的页面引入了layui.css 和 layui.js ,内层页面 切记不要再次引入 2. 具体代码如下 layui.use(['form', 'upload'], function(){ var form = layui.form; form.render(); // 加入这一句});...
2024-11-09 前端开发问题
313

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

Rails 3.1 ajax:成功处理
Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)...
2024-04-20 前端开发问题
11

ExecJS::ProgramError: SyntaxError: 保留字“function"
ExecJS::ProgramError: SyntaxError: Reserved word quot;functionquot;(ExecJS::ProgramError: SyntaxError: 保留字“function)...
2024-04-20 前端开发问题
13

无限滚动和 will_paginate 多次附加项目的“下一页"
Infinite scroll and will_paginate appending the #39;next page#39; of items multiple times(无限滚动和 will_paginate 多次附加项目的“下一页)...
2024-04-20 前端开发问题
8