首页 > 解决方案 > 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具有项目位置的正确坐标,但计算出的距离完全偏离。

标签: ruby-on-railsrubygeocoder

解决方案


不知道为什么@object.distance_to()给了我错误的距离,但我最终选择了Geocoder::Calculations.distance_between(),这给了我正确的价值。


推荐阅读