3

我正在尝试在 rails 应用程序中使用 geokit-rails3 gem 获取地理位置信息。我有一个简单的模型:

class Location < ActiveRecord::Base
  def lookup_ip_information(post_ip)
    ip = post_ip
    location = IpGeocoder.geocode(ip)
    puts location.full_address
    lat = location.lat
    lng = location.lng
  end
end

当我request.remote_ip从控制器调用此方法时,它会引发错误:uninitialized constant Location::IpGeocoder

4

2 回答 2

7

我刚刚遇到这个问题并通过输入完整的解决方案

Geokit::Geocoders::IpGeocoder.geocode(request.remote_ip)

这似乎为我解决了这个问题。

于 2011-03-31T00:13:20.073 回答
1

有同样的问题,无法通过上述建议让 IpGeocoder 在 Rails3 中工作。挖掘 gem 的 Rails3 源代码。看起来解决这个问题的一种简单方法是使用 Rails 3 的 Geokit MultiGeocoder 方法。

示例: loc = Geokit::Geocoders::MultiGeocoder.geocode(remote.request_ip)

不要忘记在初始化程序中正确设置 MultiGeocoder - geokit_config.rb Geokit::Geocoders::provider_order = [:google,:us]

于 2016-01-06T23:46:31.153 回答