2015-04-05 116 views
0

我想將兩個IPv4地址轉換爲經度和緯度,然後計算它們之間的地理距離。Rails:Geokit錯誤地將IPv4地址轉換爲經度和緯度

Geokit說它可以做到這一點。

西雅圖:

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

a.lat => 37.0483944,a.lng => 37.3421261(應爲47.5839,-122.2995)

編輯:下面好的是IP地址英國不是SF,但它仍然是英國而不是土耳其。

舊金山:

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

a.lat => 14.6478672,a.lng => 120.9880659(應爲37.7691,-122.4449)

a.distance_to(b)返回5273.737623217472。

西雅圖顯然到聖弗朗西斯科有點超過5000英里,我得到很多機票。

我錯過了什麼?

+0

檢查更新的答案,瞭解如何通過ip查找位置。 – 2015-04-05 18:02:27

+0

GeoIP永遠不會完全準確,並且會出現像這樣的錯誤。不存在每個IP地址準確映射到確切位置的數據庫。 – 2015-04-06 10:00:25

+0

@SanderSteffann謝謝你。如果這種方法本身容易出錯,那麼除了將twp IP轉換爲lat,lng和計算距離外,您是否知道檢測VPN使用的最佳做法? – gr8scott06 2015-04-07 03:11:56

回答

0

ab如您使用的是不符合SeattleSan Franscisco

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

嘗試:

> 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 = ... 

https://github.com/alexreisner/geocoder/blob/091032059e6b47285e2be5ef82ad8b016f362069/test/unit/lookups/freegeoip_test.rb的使用示例。

+0

你是否建議我使用gem'geocoder'?查看該文檔以確定可以運行的距離:a = Geocoder.coordinates(「207.244.147.34」); b = Geocoder.coordinates(「5.68.123.155」); distance = Geocoder :: Calculations.distance_between(a,b) – gr8scott06 2015-04-05 20:00:22

+0

在「避免不必要的API請求」下的地理編碼器文檔中提到了api使用配額,但未指定數量。你知道配額還是遇到問題?謝謝 – gr8scott06 2015-04-05 20:02:46