如何在Rails方法中使用HTML坐标
Ruby on Rails - How to Use HTML Coordinates in a Rails Method?
感谢回答我问题的人。
我有一个方法,目前工作像这样的post.distance_to([current_user.latitude, current_user.longitude])
。它返回post(带有地址)和current_user之间的距离。
我在用户模型中创建了属性纬度和经度,因为我不知道如何直接使用我用以下脚本检索的这些坐标:
<p id="geoloc"></p>
<script>
var x = document.getElementById("geoloc");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getCoordinates);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function getCoordinates(position) {
var new_latitude = position.coords.latitude;
var new_longitude = position.coords.longitude;
$(document).ready(function () {
$("#latitude_field").val(new_latitude);
$("#longitude_field").val(new_longitude);
}
}
getLocation()
</script>
目前我正在更新用户模型与新的坐标通过按下一个按钮使用这个表单:
<%= form_for(current_user) do |f| %>
<%=f.hidden_field :latitude ,:id=> "latitude_field" %>
<%=f.hidden_field :longitude ,:id=> "longitude_field" %>
<%=f.submit "I'm here", class: "btn btn-small btn-primary jsbuttons"%>
<%end%>
我想做的是像这样直接调用方法post.distance_to(["latitude obtained with the script", "longitude obtained with the script"])
。如何让rails知道这些坐标呢?能否使用表单中使用的javascript id获取值并将其放入方法中?谢谢!
如果这太复杂,我是否可以在每次刷新页面时使用新的坐标来更新用户模型,而无需按下按钮?(我试图为表单创建一个id,并在$(document).ready(function)
中提交该id,但它没有工作)
为您的表单使用以下代码:
<%= form_tag '/your_url' do |f| %>
<%=f.text_field_tag :latitude, params[:latitude], :id=> "latitude_field" %>
<%=f.text_field :longitude, params[:longitude], :id=> "longitude_field" %>
<%=f.submit "I'm here", class: "btn btn-small btn-primary jsbuttons"%>
<% end %>
现在在你的控制器操作中,你使用:
访问lat/longlat = params[:latitude]
lng = params[:longitude]
distance = post.distance_to([lat,lng])
相关文章:
- 将坐标传递到HTML页面以在新窗口中打开谷歌街景
- 将html元素定位在页面中的任何x/y坐标处
- HTML / JS地理位置和比较坐标
- HTML 5 画布:如何检查所有线点坐标
- 将街道地址转换为坐标并在 html 中放置
- 根据html中所需的坐标显示图像
- 用鼠标点击HTML画布绘制图像,保存坐标但不提交
- 如何使用HTML、CSS、Javascript和If Useful JQuery创建可点击分区的坐标网格
- 获取HTML画布上两个不同位置的坐标
- 为什么HTML/SVG坐标0,0从左上角开始
- 在html中的屏幕坐标(x,y)处选择一个单词
- 查找所有html链接的坐标
- 如何从HTML文件中获得一组坐标的相应元素?
- 在html文件中嵌入svg坐标的最佳方法是什么?
- SVG空间中foreignObject HTML元素的坐标
- 如何在Rails方法中使用HTML坐标
- 鼠标坐标在HTML页面上
- Html /javascript画布高度/宽度坐标不一样
- 如果我有鼠标的x和y坐标,我可以创建一个html元素吗
- 从HTML网页中的Google Maps iframe获取坐标