我使用的是Rails 4.2.0和Ruby 2.2.0。我想使用geokit将地址转换为纬度和经度。我已经在我的终端上安装了gem install geokit。我还包括了gem' geokit'在我的Gemfile中,运行bundle install。
在控制器上,我尝试使用Geokit :: Geocoders :: GoogleGeocoder.geocode()函数。
当我这样使用它时,我得到了这个错误:uninitialized constant AnswersController :: Geocoders(我的控制器叫做Answers)
当我尝试添加require' geokit'在控制器的开头,我收到了错误:无法加载这样的文件--geokit。
你知道我能做些什么来解决这个问题吗?
提前致谢
这是我的控制器
require 'rubygems'
require 'geokit'
class AnswersController < ApplicationController
before_action :set_answer, only: [:show, :edit, :update, :destroy]
def render_page2
render('page2')
end
def answer_page2
if params[:address_origin] && params[:address_destination]
session[:lat_origin] = Geokit::Geocoders::GoogleGeocoder.geocode(params[:address_origin]).lat
session[:lon_origin] = Geokit::Geocoders::GoogleGeocoder.geocode(params[:address_origin]).lng
session[:lat_destination] = Geokit::Geocoders::GoogleGeocoder.geocode(params[:address_destination]).lat
session[:lon_destination] = Geokit::Geocoders::GoogleGeocoder.geocode(params[:address_destination]).lng
#session[:lat_origin] = 37.793688
#session[:lon_origin] = -122.3958692
#session[:lat_destination] = 37.866437
#session[:lon_destination] = -122.265415
redirect_to '/page3'
else
flash[:message] = "All the questions are required on this page."
redirect_to '/page2'
end
end
end
答案 0 :(得分:1)
我已经在我的终端上安装了gem install geokit。我还包括宝石 我的Gemfile中的'geokit',并运行bundle install。
无需同时执行这两项操作。
请按照以下步骤确保您已正确安装geokit
gem:
一个。在Gemfile中添加geokit
gem
# Gemfile
gem 'geokit'
湾运行bundle install
;使用bundle show geokit
验证gem已安装。
℃。在rails控制台中尝试以下操作:
$ rails console
> a=Geokit::Geocoders::GoogleGeocoder.geocode '140 Market St, San Francisco, CA'
=> #<Geokit::GeoLoc:0x007fe877305c70 @all=[#<Geokit::GeoLoc:0x007fe877305c70 ...>], @street_address="140 Market Street", @sub_premise=nil, @street_number="140", @street_name="Market Street", @city="San Francisco", @state=nil, @state_code="CA", @state_name="California", @zip="94105", @country_code="US", @province="CA", @success=true, @precision="address", @full_address="140 Market Street, San Francisco, CA 94105, USA", @lat=37.793688, @lng=-122.3958692, @provider="google", @neighborhood="Financial District", @district="San Francisco County", @country="United States", @accuracy=8, @suggested_bounds=#<Geokit::Bounds:0x007fe8772fef60 @sw=#<Geokit::LatLng:0x007fe8772fef88 @lat=37.7923338697085, @lng=-122.3972116302915>, @ne=#<Geokit::LatLng:0x007fe8772ff050 @lat=37.7950318302915, @lng=-122.3945136697085>>>
> a.ll
=> "37.793688,-122.3958692"
答案 1 :(得分:1)
我想我找到了解决方案。代码很好,Prakash发布的所有步骤都很好。
但是我需要在运行这些步骤后重新启动终端上的rails服务器,这就是为什么它不能在浏览器上运行!现在它正在发挥作用。
感谢大家的回答。我不知道这是否是最好的解决方案,但至少它是有效的。