0

I want to convert two IPv4 addresses to latitude and longitude, then calculate the geographical distance between them.

Geokit says it can do this.

Seattle:

a = Geokit::Geocoders::GoogleGeocoder.geocode("207.244.147.34")

a.lat => 37.0483944, a.lng => 37.3421261 (should be 47.5839, -122.2995)

Edit: Okay below is an IP address for UK not SF, but its still UK not Turkey.

San Francisco:

b = Geokit::Geocoders::GoogleGeocoder.geocode("5.68.123.155")

a.lat => 14.6478672, a.lng => 120.9880659 (should be 37.7691, -122.4449)

a.distance_to(b) returns 5273.737623217472.

So apparently Seattle to San Fransisco is bit over 5000 miles and I am getting a great deal on airfare.

What am I missing?

gr8scott06
  • 903
  • 1
  • 11
  • 20

1 Answers1

0

a and b as you are using does not correspond to Seattle and San Franscisco:

> a.city
 => "Gaziantep" 
> a.country
 => "Turkey" 
> b.city
 => "Caloocan" 
> b.country
 => "Philippines"

Try:

> Geocoder.configure(ip_lookup: :freegeoip)
> a = Geocoder.search("74.200.247.59").first
 =>  #<Geocoder::Result::Freegeoip:0x007fc6df3cf930 @data={"ip"=>"207.244.147.34", "country_code"=>"US", "country_name"=>"United States", "region_code"=>"WA", "region_name"=>"Washington", "city"=>"Seattle", "zip_code"=>"98103", "time_zone"=>"America/Los_Angeles", "latitude"=>47.674, "longitude"=>-122.342, "metro_code"=>819}, @cache_hit=nil>
 > b = ...

See https://github.com/alexreisner/geocoder/blob/091032059e6b47285e2be5ef82ad8b016f362069/test/unit/lookups/freegeoip_test.rb for the usage example.

Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74
  • Are you suggesting I use gem 'geocoder'? Looking at that documentation to determine distance I can run: a = Geocoder.coordinates("207.244.147.34"); b = Geocoder.coordinates("5.68.123.155"); distance = Geocoder::Calculations.distance_between(a,b) – gr8scott06 Apr 05 '15 at 20:00
  • In the geocoder docs under "Avoiding Unnecessary API Requests" it mentions an api usage quota but doesn't specify the amount. Do you know the quota or have run into problems with that? Thanks – gr8scott06 Apr 05 '15 at 20:02