How to calculate distance in km between two points using Geocoder
Asked Answered
L

1

15

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:

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:

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)
Lipophilic answered 19/9, 2015 at 19:57 Comment(0)
M
29

In Geocoder::Calculations module, there is a method called 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:

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)
Memento answered 19/9, 2015 at 20:29 Comment(5)
@JędrekDomański I have updated my answer accordinglyMemento
Thanks , but for some latlong distance is not correct. Can u please suggest what is the reason . I am finding distance between latlongs latitude=19.1092883 longitude=73.0131332 latitude=19.115710 longitude=72.918257 result => around 10 km, but expected/correct result should be around 18 kmMadalinemadalyn
@SnmMaurya where are these coordinates come from are they stored in the database or what is the source. if they are from the database then you need to make sure that you are storing them as decimal to get more accurate resultsMemento
Thanks, Actually I got the reason it does not calculate distance via "travel way" it calculates directly. So is there any way in goecoder to get distance via "travel way".Madalinemadalyn
@SnmMaurya Geocoder default distance unit is Miles, you can change it to kilometers in geocoder config file.Lipophilic

© 2022 - 2024 — McMap. All rights reserved.