문제

So I am using Geocoder to pull in latitude and longitude coordinates based on an address provided by a user when submitting a form. I am doing this so that I can plot markers using the Google Maps API. This works perfectly in development - zero issues. When I push to production however, the latitude and longitude are not generated by Geocoder. I have checked my production logs and there are zero errors. I also checked to make sure that the gem is not just installed for development and I have restarted unicorn and nginx on my production machine. Any ideas as to what could be going on?

Here is my model code for the model I am preforming this task in --

class Order < ActiveRecord::Base

  attr_accessible :city, :customer_email, :customer_id, :delivery_date, :delivery_time, :street, :zipcode, :coupon, :total, :name, :items_with_day, :user_id, :shopping_cart_id, :extras, :latitude, :longitude, :location_name, :phone, :suite, :state

  belongs_to :user

  validates :name, :presence => true
  validates :street, :presence => true
  validates :city, :presence => true
  validates :zipcode, :presence => true

  def address
    [street, city, state, "United States"].compact.join(', ')
  end

  geocoded_by :address
  after_validation :geocode

end

============ UPDATE =============

So after a suggestion, I opened up the rails console on my production machine using --

bundle exec rails console production

and ran --

Geocoder.search("San Francisco, CA")

which generated this error --

Geocoding API not responding fast enough (use Geocoder.configure(:timeout => ...) to set limit).
=> []

that being said, in my config/initilizers/geocoder.rb file, I have --

Geocoder::Configuration.timeout = 15

What is an appropriate timeout limit to set? I need the coordinates, but also don't want to let this run forever...

=========== UPDATE =============

I found this related question, though I would prefer to run this task server side, instead of on the client side.

도움이 되었습니까?

해결책 2

The author of the Geocoder gem has this to say:

[A]re you sure the app server was restarted since you added Geocoder?

First, verify that the gem is indeed installed by issuing a Geocoder command in your production console:

# from the production console
Geocoder.search("San Francisco, CA")

If this does not throw any errors, try restarting the server. A soft boot might work – a hard boot is better. This will reload your entire Rails environment, inclusive of any installed dependencies.

UPDATE:

As mentioned in the comments to this answer, the problem is related to the Geocoding API not responding fast enough error.

How long is the appropriate timeout?

That's hard to say. The gem's author suggests 15, as you already have it set to. The geocoding service may be momentarily slow – or it may be slow overall. IMO, set it incrementally higher and find the number where it starts working – then leave it there.

Note that the client-side vs. server-side argument may not apply here. The SO post cited in the OP's question relates to request quotas. The problem being witnessed in the question, in contrast, seems to relate to geocoding speed, which would not be affected whether the calls are made by the client or by the server.

다른 팁

Your initializer might not be loaded in a production environment. Try running your console command (bundle exec rails console production) and run your Geocoder::Configuration.timeout = 15 in the console, then try your test command again.

I think it is an issue with freegeoip, try the following:

Enter this in your rails console:

Geocoder.configure(:ip_lookup => :telize)

Now try you search in your console.

if this works you can have your file (config/initializers/geocoder.rb)

Geocoder.configure(

  :timeout=>20,

  :lookup=>:yandex,

  :ip_lookup=>:telize,

  :language=>:en,

  :http_headers=>{},

  :use_https=>false,

  :http_proxy=>nil,

  :https_proxy=>nil,

  :api_key=>nil,

  :cache=>nil,

  :cache_prefix=>"geocoder:",

  :units=>:km,

  :distances=>:linear

)

I hope this help you.

Check your network connection. I found that when I went to a faster connection the issue was alleviated. In my case, I went from a public wifi connection at a coffee shop to my LTE hotspot on my phone.

From the Google guys

*We recommend that applications that respond to user input, and therefore are highly latency-sensitive, use the Place Autocomplete feature in the Places API (also available in JavaScript, Android, or iOS) rather than address geocoding. Place Autocomplete is optimized to be used interactively, and thus has very low latency.

Address geocoding in the Geocoding API is optimized for use with complete, unambiguous, well formatted addresses, such as delivery addresses entered into online forms, and thus has higher latency than Place Autocomplete. This is already the case with the old forward geocoder. The latency difference between Place Autocomplete and the new forward geocoder increases further, since the new geocoder has greater coverage and better result quality, but at the cost of somewhat higher latency.*

https://developers.google.com/maps/documentation/geocoding/faq

You have to setup a lookup service on config/initializers/geocoder.rb for production such as google, yandex, etc. Otherwise it won't return the coordinates for all your request.

Geocoder.configure(
  timeout: 3,
  lookup: :google,
  api_key: ENV['GOOGLE_MAPS_API_KEY'],
  units: :km
)

If you set it to work with Google make sure to create an API account on https://console.developers.google.com to get the API key or it won't work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top