Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active October 26, 2021 02:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/30b144ddda7f7f7fccbce365ca454686 to your computer and use it in GitHub Desktop.
Save JoshCheek/30b144ddda7f7f7fccbce365ca454686 to your computer and use it in GitHub Desktop.
Distance between locations, given their names
loc1_name = 'Chicago' # => "Chicago"
loc2_name = 'Houston' # => "Houston"
# Get the lib with `gem install geocoder`
# it ultimately calls out to this API: http://maps.googleapis.com/maps/api/geocode/json?address=Chicago&language=en&sensor=false
require 'geocoder' # => true
include Math # => Object
def radians(n)
n * PI / 180 # => 0.7309109668442155, -1.5294285014482003, 0.5194174327134308, -1.664517065837707
end # => :radians
alias √ sqrt
loc1 = Geocoder.search(loc1_name)[0] # => #<Geocoder::Result::Google:0x007fad839c5d50 @data={"address_components"=>[{"long_name"=>"Chicago", "short_name"=>"Chicago", "types"=>["locality", "political"]}, {"long_name"=>"Cook County", "short_name"=>"Cook County", "types"=>["administrative_area_level_2", "political"]}, {"long_name"=>"Illinois", "short_name"=>"IL", "types"=>["administrative_area_level_1", "political"]}, {"long_name"=>"United States", "short_name"=>"US", "types"=>["country", "political"]}], "formatted_address"=>"Chicago, IL, USA", "geometry"=>{"bounds"=>{"northeast"=>{"lat"=>42.023131, "lng"=>-87.52366099999999}, "southwest"=>{"lat"=>41.6443349, "lng"=>-87.9402669}}, "location"=>{"lat"=>41.8781136, "lng"=>-87.6297982}, "location_type"=>"APPROXIMATE", "viewport"=>{"northeast"=>{"lat"=>42.023131, "lng"=>-87.52404399999999}, "southwest"=>{"lat"=>41.6443349, "lng"=>-87.9402669}}}, "place_id"=>"ChIJ7cv00DwsDogRAMDACa2m4K8", "types"=>["locality", "political"]}, @cache_hit=nil>
lat1 = radians loc1.latitude # => 0.7309109668442155
lon1 = radians loc1.longitude # => -1.5294285014482003
loc2 = Geocoder.search(loc2_name)[0] # => #<Geocoder::Result::Google:0x007fad839edf08 @data={"address_components"=>[{"long_name"=>"Houston", "short_name"=>"Houston", "types"=>["locality", "political"]}, {"long_name"=>"Harris County", "short_name"=>"Harris County", "types"=>["administrative_area_level_2", "political"]}, {"long_name"=>"Texas", "short_name"=>"TX", "types"=>["administrative_area_level_1", "political"]}, {"long_name"=>"United States", "short_name"=>"US", "types"=>["country", "political"]}], "formatted_address"=>"Houston, TX, USA", "geometry"=>{"bounds"=>{"northeast"=>{"lat"=>30.1107319, "lng"=>-95.014496}, "southwest"=>{"lat"=>29.523624, "lng"=>-95.78808690000001}}, "location"=>{"lat"=>29.7604267, "lng"=>-95.3698028}, "location_type"=>"APPROXIMATE", "viewport"=>{"northeast"=>{"lat"=>30.1107319, "lng"=>-95.014496}, "southwest"=>{"lat"=>29.523624, "lng"=>-95.78808690000001}}}, "place_id"=>"ChIJAYWNSLS4QIYROwVl894CDco", "types"=>["locality", "political"]}, @cache_hit=nil>
lat2 = radians loc2.latitude # => 0.5194174327134308
lon2 = radians loc2.longitude # => -1.664517065837707
# "haversine" formula, found @ http://www.movable-type.co.uk/scripts/latlong.html
∆lat = lat1 - lat2 # => 0.21149353413078475
∆lon = lon1 - lon2 # => 0.13508856438950678
a = sin(∆lat/2)**2 + cos(lat1) * cos(lat2) * (sin(∆lon/2)**2) # => 0.014085141202494494
c = 2 * atan2(√(a), √(1-a)) # => 0.23792244890858025
r = 3_959 # radius of the earth in miles
# Expected: 941 miles (based on https://www.timeanddate.com/worldclock/distances.html?n=104)
distance = r * c # => 941.9349752290692
@dzntz
Copy link

dzntz commented Aug 12, 2020

Lacey ain't no alpha

@phadytou
Copy link

Thank you

@phadytou
Copy link

Hello word

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment