LoginSignup
15
16

More than 5 years have passed since last update.

Rails5 で geocoder を使って、緯度経度の取得する(Google API の設定)

Posted at

Rails5 で地図アプリを作成するため、gmap4rails, geocoder を実験しました。緯度経度が取得できず苦労しましたが、APIの設定を見直すことで成功したので記録に残します。

Rails GoogleMap表示 gem gmaps4railsとgeocoder を参照に地図アプリを作成する。
https://qiita.com/yuki_chrono/items/a2638c33eedc3c036d01

作成したアプリで地図は表示できたが、緯度経度が取得できない場合があった。「埼玉県」なら取得できるが「埼玉県さいたま市」にすると取得できない...

snap_1215.png

どうやら geocoder 単体では、細かい緯度経度が取得できないらしい。Google geocoding を利用することで、(Google で調べられる地点なら)すべての場所の緯度経度を取得することができるようになる。

geocoder 設定ファイルを作成

$ bundle exec rails generate geocoder:config
config/initializers/geocoder.rb
Geocoder.configure(

  # street address geocoding service (default :nominatim)
  lookup: :google,

  # IP address geocoding service (default :ipinfo_io)
  # ip_lookup: :maxmind,

  # to use an API key:
  api_key: "AIzaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",

  # geocoding service request timeout, in seconds (default 3):
  timeout: 5,

  # set default units to kilometers:
  units: :km,

)

コンソールで動作確認

$ bundle exec rails console
![snap_1214.png](https://qiita-image-store.s3.amazonaws.com/0/320718/236a5d2c-c062-6fa6-a04b-d31ed4e963be.png)

irb(main):002:0> Geocoder.search("東京タワー")
Google API error: request denied (API keys with referer restrictions cannot be used with this API.).
=> []
irb(main):003:0> Geocoder.coordinates('埼玉県さいたま市')
Google API error: request denied (API keys with referer restrictions cannot be used with this API.).
=> nil

エラーメッセージ
Google API error: request denied (API keys with referer restrictions cannot be used with this API.).
をヒントにAPIの設定を見直すことで緯度経度を取得できるようになった。原因は(gmaps4railsのAPI設定と同じように)「HTTP リファラー」で制限をかけていたためだった。アプリケーションの制限を「なし」に変更する。

snap_1214.png

irb(main):005:0> Geocoder.search("東京タワー")
=> [#<Geocoder::Result::Google:0x00007faf8616c7f8 @data={"address_components"=>[{"long_name"=>"8", "short_name"=>"8", "types"=>["premise"]}, {"long_name"=>"2", "short

_name"=>"2", "types"=>["political", "sublocality", "sublocality_level_4"]}, {"long_name"=>"4 Chome", "short_name"=>"4 Chome", "types"=>["political", "sublocality", "s
ublocality_level_3"]}, {"long_name"=>"Shibakoen", "short_name"=>"Shibakoen", "types"=>["political", "sublocality", "sublocality_level_2"]}, {"long_name"=>"Minato", "s
hort_name"=>"Minato", "types"=>["locality", "political"]}, {"long_name"=>"Tokyo", "short_name"=>"Tokyo", "types"=>["administrative_area_level_1", "political"]}, {"lon
g_name"=>"Japan", "short_name"=>"JP", "types"=>["country", "political"]}, {"long_name"=>"105-0011", "short_name"=>"105-0011", "types"=>["postal_code"]}], "formatted_a
ddress"=>"4 Chome-2-8 Shibakoen, Minato, Tokyo 105-0011, Japan", "geometry"=>{"location"=>{"lat"=>35.6585805, "lng"=>139.7454329}, "location_type"=>"ROOFTOP", "viewpo
rt"=>{"northeast"=>{"lat"=>35.6599294802915, "lng"=>139.7467818802915}, "southwest"=>{"lat"=>35.6572315197085, "lng"=>139.7440839197085}}}, "place_id"=>"ChIJCewJkL2LG
GAR3Qmk0vCTGkg", "plus_code"=>{"compound_code"=>"MP5W+C5 Tokyo, Japan", "global_code"=>"8Q7XMP5W+C5"}, "types"=>["establishment", "point_of_interest", "premise"]}, @c
ache_hit=nil>]
irb(main):006:0> Geocoder.coordinates('埼玉県さいたま市')
=> [35.8617292, 139.6454822]

(注意)
gmaps4rails のAPI KEY は「HTTP リファラー」で制限をかける必要がある点に注意が必要です。

snap_1216.png

15
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
16