简体   繁体   English

如何在Rails项目上使用Geokit

[英]How to use Geokit on a rails project

I am using Rails 4.2.0 and Ruby 2.2.0. 我正在使用Rails 4.2.0和Ruby 2.2.0。 I want to use geokit to convert an address to latitude and longitude. 我想使用geokit将地址转换为纬度和经度。 I already did gem install geokit in my terminal. 我已经在终端中安装了gemkit geokit。 I also included gem 'geokit' in my Gemfile, and run bundle install. 我还在我的Gemfile中添加了gem'geokit',然后运行bundle install。

On a controller, I try to use the function Geokit::Geocoders::GoogleGeocoder.geocode(). 在控制器上,我尝试使用功能Geokit :: Geocoders :: GoogleGeocoder.geocode()。

When I just use it like this, I got this error : uninitialized constant AnswersController::Geocoders (my controller is called Answers) 当我像这样使用它时,出现以下错误:未初始化的常量AnswersController :: Geocoders(我的控制器称为Answers)

When I try to add require 'geokit' at the beginning of the controller, I got the error : cannot load such file --geokit. 当我尝试在控制器的开头添加require'geokit'时,出现错误:无法加载--geokit这样的文件。

Do you have any idea of what I could do to solve the problem ? 您对我能解决这个问题有什么想法吗?

Thanks in advance 提前致谢

Here is my controller 这是我的控制器

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

I already did gem install geokit in my terminal. 我已经在终端中安装了gemkit geokit。 I also included gem 'geokit' in my Gemfile, and run bundle install. 我还在我的Gemfile中添加了gem'geokit',然后运行bundle install。

No need for doing both. 无需同时执行这两项操作。

Follow these steps to make sure you have installed the geokit gem correctly: 请按照以下步骤操作,以确保正确安装了geokit gem:

a. 一种。 Add geokit gem in the Gemfile 在Gemfile中添加geokit gem

# Gemfile
gem 'geokit'

b. b。 Run bundle install ; 运行bundle install ; verify the gem is installed with bundle show geokit . 验证gem是否与bundle show geokit一起安装。

c. C。 Try the following in the rails console: 在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"

I think I found the solution. 我想我找到了解决方案。 The code is good and all the steps posted by Prakash are good. 代码很好,Prakash发布的所有步骤都很好。

But I needed to restart the rails server on the terminal after you run those steps, that is why it was not working on the browser ! 但是运行这些步骤后,我需要在终端上重新启动Rails服务器,这就是为什么它在浏览器上不起作用的原因! Now it is working. 现在正在工作。

Thanks everyone for your answers. 谢谢大家的回答。 I don't know if it is the best solution, but at least it works. 我不知道这是否是最好的解决方案,但至少可以奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM