ruby-on-railsgeocoderails-geocoder

is the total of a lat / long calculation specify the map distance?


We use the rails geocoder gem at work but want to start getting closest location of a set of locations that are stored in memcached (ie not using the ActiveRecord interface). Would it be as simple as just calculating the numerical difference between our starting point and our different place points? Or are there other calculations that I should be aware of?

This is only for map distance and not street distance or traffic distance. Does geocoder support this calculation?


Solution

  • Geocoder::Calculations.distance_to can be used to get the distance between two points with the haversine formula:

    coordinates = [[1, 1], [-2, 1], [3,2]]
    hash = coordinates.each_with_object({}) do |latlng, memo|
      memo[latlng] = Geocoder::Calculations.distance_between(latlng, location)
    end
    
    hash.min # [[-2, 1], 154.49109373250715]
    hash.max # [[3, 2], 249.0844867974628]
    

    The haversine formula is gerally good enough for most uses but doesn't account for that fact that earth is a less then perfect sphere and it cant be guaranteed correct to better than 0.5%.