Rails: Geokit incorrectly converting IPv4 address to latitude and longitude

这一生的挚爱 提交于 2019-12-25 05:25:36

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29460004/rails-geokit-incorrectly-converting-ipv4-address-to-latitude-and-longitude

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!