簡體   English   中英

如何在Rails項目上使用Geokit

[英]How to use Geokit on a rails project

我正在使用Rails 4.2.0和Ruby 2.2.0。 我想使用geokit將地址轉換為緯度和經度。 我已經在終端中安裝了gemkit 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

我已經在終端中安裝了gemkit geokit。 我還在我的Gemfile中添加了gem'geokit',然后運行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"

我想我找到了解決方案。 代碼很好,Prakash發布的所有步驟都很好。

但是運行這些步驟后,我需要在終端上重新啟動Rails服務器,這就是為什么它在瀏覽器上不起作用的原因! 現在正在工作。

謝謝大家的回答。 我不知道這是否是最好的解決方案,但至少可以奏效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM