title: How to calculate distance in km between two points using Geocoder
body: <p>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:</p>
  
  <pre><code>class Ride &lt; ActiveRecord::Base
      geocoded_by :start_address
      geocoded_by :destination_address
      reverse_geocoded_by :latitude, :longitude
      after_validation :geocode
  
  end
  </code></pre>
  
  <p>How can I get calculated distance between the two points and show in view. No need for longitude and latitude.</p>
  
  <p>Edit:
  The default unit is 'miles'. To change default unit set it in your ActiveRecord model:</p>
  
  <pre><code>Venue.near([40.71, -100.23], 20, :units =&gt; :km)
  </code></pre>
  
  <p>or change it in:</p>
  
  <pre><code># config/initializers/geocoder.rb
  # set default units to kilometers:
  :units =&gt; :km,
  </code></pre>
  
  <p>Edit:
  I've managed to resolve this like so:</p>
  
  <pre><code>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)
  </code></pre>