问题描述
我正在使用 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()
,这给了我正确的值。