I'm trying to figure out a way to show calculated distance between two point on a map using Geocoder. I have start_address and destination_address in my table which I ask for in my view, my model looks like this:
我正试图找出一种方法,使用Geocoder显示地图上两点之间的计算距离。我在我的表中有start_address和destination_address,我在我的视图中要求,我的模型如下所示:
class Ride < ActiveRecord::Base
geocoded_by :start_address
geocoded_by :destination_address
reverse_geocoded_by :latitude, :longitude
after_validation :geocode
end
How can I get calculated distance between the two points and show in view. No need for longitude and latitude.
如何计算两点之间的距离并显示在视图中。不需要经度和纬度。
Edit: The default unit is 'miles'. To change default unit set it in your ActiveRecord model:
编辑:默认单位为“里程”。要更改默认单位,请在ActiveRecord模型中设置它:
Venue.near([40.71, -100.23], 20, :units => :km)
or change it in:
或者改变它:
# config/initializers/geocoder.rb
# set default units to kilometers:
:units => :km,
Edit: I've managed to resolve this like so:
编辑:我设法解决这个问题:
ride = Ride.new(params)
start_address_coordinates = Geocoder.coordinates(params[:start_address])
destination_coordinates = Geocoder.coordinates(params[:destination])
ride.distance = Geocoder::Calculations.distance_between(start_address_coordinates, destination_coordinates)
1 个解决方案
#1
20
In Geocoder::Calculations
module, there is a method called distance_between(lat1, lon1, lat2, lon2, options = {})
在Geocoder :: Calculations模块中,有一个名为distance_between的方法(lat1,lon1,lat2,lon2,options = {})
you pass the longitude and latitude of two points and it gives you back the distance between these two points.
你传递两点的经度和纬度,它会让你回到这两点之间的距离。
For further info please check this link out
有关详细信息,请查看此链接
quoted from theGem's docs
:
引自theGem的文档:
look up coordinates of some location (like searching Google Maps)
Geocoder.coordinates("25 Main St, Cooperstown, NY")
=> [42.700149, -74.922767]
So you can use this above method to get the coordinates of a specific location entered by the user, then you can calculate the difference in distance between two points by the below method.
因此,您可以使用上述方法获取用户输入的特定位置的坐标,然后您可以通过以下方法计算两点之间的距离差异。
distance between Eiffel Tower and Empire State Building
Geocoder::Calculations.distance_between([47.858205,2.294359], [40.748433,-73.985655])
=> 3619.77359999382 # in configured units (default miles)
#1
20
In Geocoder::Calculations
module, there is a method called distance_between(lat1, lon1, lat2, lon2, options = {})
在Geocoder :: Calculations模块中,有一个名为distance_between的方法(lat1,lon1,lat2,lon2,options = {})
you pass the longitude and latitude of two points and it gives you back the distance between these two points.
你传递两点的经度和纬度,它会让你回到这两点之间的距离。
For further info please check this link out
有关详细信息,请查看此链接
quoted from theGem's docs
:
引自theGem的文档:
look up coordinates of some location (like searching Google Maps)
Geocoder.coordinates("25 Main St, Cooperstown, NY")
=> [42.700149, -74.922767]
So you can use this above method to get the coordinates of a specific location entered by the user, then you can calculate the difference in distance between two points by the below method.
因此,您可以使用上述方法获取用户输入的特定位置的坐标,然后您可以通过以下方法计算两点之间的距离差异。
distance between Eiffel Tower and Empire State Building
Geocoder::Calculations.distance_between([47.858205,2.294359], [40.748433,-73.985655])
=> 3619.77359999382 # in configured units (default miles)