簡體   English   中英

使用Geocoder gem通過IP搜索位置無法在Rails 4上正常工作

[英]Searching location by IP using Geocoder gem not working properly with rails 4

我在Rails應用程序中添加了geocoder gem,但是當我通過IP搜索位置時,它無法正確響應。

問題在於它正在返回nil數組。

Geocoding API's response was not valid JSON.
[]

但是有時它可以正常工作,我不知道為什么。

我做了什么:

的Gemfile

gem 'geocoder'

初始化/ geocoder.rb

Geocoder.configure(
    :timeout  => 20,
    :lookup   => :google,
    #:ip_lookup => :google,
    #:api_key  => "xxxx-xxx",
    :units    => :km
)

application_controller.rb

def current_location
 if Rails.env.development?
  geo_data = Geocoder.search('50.131.44.114')
 else
  geo_data = Geocoder.search(request.remote_ip)
 end
 [geo_data[0].city, geo_data[0].country_code ].compact.join(', ') 
end

使用此“ Geocoder.configure(ip_lookup::telize)”遇到相同的錯誤,它解決了該問題。 來源-https://github.com/alexreisner/geocoder/issues/770

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM