ruby-on-rails - Ruby Geocoder Gem 给出不准确的距离
问题描述
我正在使用 Geocoder gem 来计算某些位置与用户位置之间的距离。我还手动插入坐标来测量距离并且值完全关闭。
序列化模型内部:
attribute :distance_to_user do
if @user_location.present?
locale = @language.locale
units = (I18n.t "distance.thousand.other", default: "km").to_sym
distance = @object.distance_to(@user_location, units)
converted_distance = LocationsHelper.convert_distance(locale, distance)
ActionController::Base.helpers.number_to_human(converted_distance, units: :distance, format: "%n%u")
end
end
位置助手
def convert_distance(locale, distance)
return distance if locale.eql?("en-US")
distance * 1000
end
@user_location
为用户返回正确的坐标,并@object
具有项目位置的正确坐标,但计算出的距离完全偏离。
解决方案
不知道为什么@object.distance_to()
给了我错误的距离,但我最终选择了Geocoder::Calculations.distance_between()
,这给了我正确的价值。
推荐阅读
- php - 在 ajax 成功部分中显示数组对象值
- html - 在运行时在 Next.js 中插入基于环境的不同脚本
- python - python pandas数据框将所有元素变异为(行中的元素/最大元素)
- swift - 如何在 SwiftUI 中结合旋转和淡出动画
- python - 如何在具有多处理的for循环中获取不同函数的返回值
- python - 以最快的方式确定Python中数组中每个组重复值的索引
- android - 单击向下箭头时,我可以使用哪个颤振小部件向上滚动屏幕?
- javascript - 如何设置 Leaflet Map 的缩放以显示 React Leaflet 中的所有标记?
- php - jquery 类选择器仅适用于第一行
- emacs - 将组织模式卡住的项目定义为没有计划任务