【问题标题】:How to use Geokit on a rails project如何在 Rails 项目中使用 Geokit
【发布时间】:2015-04-04 16:28:40
【问题描述】:

我正在使用 Rails 4.2.0 和 Ruby 2.2.0。我想使用 geokit 将地址转换为纬度和经度。我已经在终端中安装了geokit。我还在我的 Gemfile 中包含 gem 'geokit',然后运行 ​​bundle install。

在控制器上,我尝试使用函数 Geokit::Geocoders::GoogleGeocoder.geocode()。

当我像这样使用它时,我得到了这个错误:未初始化的常量 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

【问题讨论】:

  • answers_controller.rb向我们展示您的代码。

标签: ruby-on-rails ruby geokit


【解决方案1】:

我已经在终端中安装了geokit。我还包括宝石 'geokit' 在我的 Gemfile 中,然后运行 ​​bundle install。

两者都不需要。

按照以下步骤确保您已正确安装 geokit gem:

一个。在 Gemfile 中添加geokit gem

# Gemfile
gem 'geokit'

b.运行bundle install;验证 gem 是否安装了 bundle show geokit

c。在 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"

【讨论】:

  • 我只是不知道如何在我的项目中使用它。我在运行 rails server 并使用 localhost 在浏览器上启动我的应用程序时遇到了这些错误
【解决方案2】:

我想我找到了解决方案。代码很好,Prakash 发布的所有步骤都很好。

但是在您运行这些步骤后,我需要在终端上重新启动 rails 服务器,这就是它无法在浏览器上运行的原因!现在它正在工作。

感谢大家的回答。我不知道这是否是最好的解决方案,但至少它有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多