住所を渡せば位置情報を教えてもらえる『Google Maps Geocoding API』の使い方

住所から緯度経度を求めたい時どうしましょう。
真っ先に思い浮かぶのはGoogleさんに教えてもらうこと!

ということで、Googleが提供する位置情報APIである「Google Maps Geocoding API」の使い方をまとめます。
なお、本家を見ればほぼ分かります。
デベロッパー ガイド  |  Google Maps Geocoding API  |  Google Developers

リクエスト形式

基本形はこんな感じです。

https://maps.googleapis.com/maps/api/geocode/output?parameters

「output」の種類

output部分の指定は大きく2種類

必須パラメータ

parametersには必須となるものがあります。

address

住所情報です。

key

Google APIを利用するための一意のキーです。取得方法は本家のガイドを参考に。
キーの取得、認証  |  Google Maps Geocoding API  |  Google Developers
※検証したところ、keyの指定が無くともレスポンスを得られるようです。

オプションだけど推奨パラメータ

lang

日本語であれば「ja」を指定。このパラメータを省略すると、「可能な限り」リクエスト元のドメインの言語情報で返すと記載されています。

実際にリクエストしてみる

Rubyでの実装例です。

#coding:utf-8
require 'net/http'
require 'openssl'
class GoogleGeocode
  GOOGLE_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxx"
  GOOGLE_GEOCODE_BASE_URL="https://maps.google.com/maps/api/geocode"
  def get_geocode(query,output)
    geocode_url = "#{GOOGLE_GEOCODE_BASE_URL}/#{output}?address=#{query}&language=ja"#&key=#{GOO      GLE_API_KEY}&language=ja"
    uri = URI.parse(URI.escape(geocode_url))
    http = Net::HTTP.new(uri.host,uri.port)
    http.use_ssl=true
    http.verify_mode=OpenSSL::SSL::VERIFY_NONE
    response_body = http.get(uri.request_uri).body
    puts response_body
  end
end
g = GoogleGeocode.new
g.get_geocode(ARGV[0],ARGV[1])

実行例

前述のrubyプログラムを使って、仙台が誇る「台原森林公園」で住所情報を取得してみます。

#xml
ruby google_geocode.rb 台原森林公園 xml
#json
ruby google_geocode.rb 台原森林公園 json

XMLの出力例

<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
 <status>OK</status>
 <result>
  <type>sublocality_level_1</type>
  <type>sublocality</type>
  <type>political</type>
  <formatted_address>日本, 〒981-0903 宮城県仙台市青葉区台原森林公園</formatted_address>
  <address_component>
   <long_name>台原森林公園</long_name>
   <short_name>台原森林公園</short_name>
   <type>sublocality_level_1</type>
   <type>sublocality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>青葉区</long_name>
   <short_name>青葉区</short_name>
   <type>ward</type>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>仙台市</long_name>
   <short_name>仙台市</short_name>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>宮城県</long_name>
   <short_name>宮城県</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>日本</long_name>
   <short_name>JP</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>981-0903</long_name>
   <short_name>981-0903</short_name>
   <type>postal_code</type>
  </address_component>
  <geometry>
   <location>
    <lat>38.2892989</lat>
    <lng>140.8787089</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>38.2871258</lat>
     <lng>140.8750561</lng>
    </southwest>
    <northeast>
     <lat>38.3013188</lat>
     <lng>140.8838157</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>38.2871258</lat>
     <lng>140.8750561</lng>
    </southwest>
    <northeast>
     <lat>38.3013188</lat>
     <lng>140.8838157</lng>
    </northeast>
   </bounds>
  </geometry>
  <place_id>ChIJQ6unShSHiV8R0E920KKkYM0</place_id>
 </result>
 <result>
  <type>park</type>
  <type>point_of_interest</type>
  <type>establishment</type>
  <formatted_address>日本, 〒981-0903 宮城県仙台市青葉区台原森林公園3 台原森林公園</formatted_address>
  <address_component>
   <long_name>台原森林公園</long_name>
   <short_name>台原森林公園</short_name>
   <type>point_of_interest</type>
   <type>establishment</type>
  </address_component>
  <address_component>
   <long_name></long_name>
   <short_name></short_name>
   <type>sublocality_level_3</type>
   <type>sublocality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>台原森林公園</long_name>
   <short_name>台原森林公園</short_name>
   <type>sublocality_level_1</type>
   <type>sublocality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>青葉区</long_name>
   <short_name>青葉区</short_name>
   <type>ward</type>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>仙台市</long_name>
   <short_name>仙台市</short_name>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>宮城県</long_name>
   <short_name>宮城県</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>日本</long_name>
   <short_name>JP</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>981-0903</long_name>
   <short_name>981-0903</short_name>
   <type>postal_code</type>
  </address_component>
  <geometry>
   <location>
    <lat>38.2953982</lat>
    <lng>140.8803943</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>38.2940492</lat>
     <lng>140.8790453</lng>
    </southwest>
    <northeast>
     <lat>38.2967472</lat>
     <lng>140.8817433</lng>
    </northeast>
   </viewport>
  </geometry>
  <place_id>ChIJj6gQcRaHiV8RIk7P2Vis4wc</place_id>
 </result>
</GeocodeResponse>

JSONの出力例

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "台原森林公園",
               "short_name" : "台原森林公園",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "青葉区",
               "short_name" : "青葉区",
               "types" : [ "ward", "locality", "political" ]
            },
            {
               "long_name" : "仙台市",
               "short_name" : "仙台市",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "宮城県",
               "short_name" : "宮城県",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "日本",
               "short_name" : "JP",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "981-0903",
               "short_name" : "981-0903",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "日本, 〒981-0903 宮城県仙台市青葉区台原森林公園",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 38.3013188,
                  "lng" : 140.8838157
               },
               "southwest" : {
                  "lat" : 38.2871258,
                  "lng" : 140.8750561
               }
            },
            "location" : {
               "lat" : 38.2892989,
               "lng" : 140.8787089
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.3013188,
                  "lng" : 140.8838157
               },
               "southwest" : {
                  "lat" : 38.2871258,
                  "lng" : 140.8750561
               }
            }
         },
         "place_id" : "ChIJQ6unShSHiV8R0E920KKkYM0",
         "types" : [ "sublocality_level_1", "sublocality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "台原森林公園",
               "short_name" : "台原森林公園",
               "types" : [ "point_of_interest", "establishment" ]
            },
            {
               "long_name" : "",
               "short_name" : "",
               "types" : [ "sublocality_level_3", "sublocality", "political" ]
            },
            {
               "long_name" : "台原森林公園",
               "short_name" : "台原森林公園",
               "types" : [ "sublocality_level_1", "sublocality", "political" ]
            },
            {
               "long_name" : "青葉区",
               "short_name" : "青葉区",
               "types" : [ "ward", "locality", "political" ]
            },
            {
               "long_name" : "仙台市",
               "short_name" : "仙台市",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "宮城県",
               "short_name" : "宮城県",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "日本",
               "short_name" : "JP",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "981-0903",
               "short_name" : "981-0903",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "日本, 〒981-0903 宮城県仙台市青葉区台原森林公園3 台原森林公園",
         "geometry" : {
            "location" : {
               "lat" : 38.2953982,
               "lng" : 140.8803943
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.2967471802915,
                  "lng" : 140.8817432802915
               },
               "southwest" : {
                  "lat" : 38.2940492197085,
                  "lng" : 140.8790453197085
               }
            }
         },
         "place_id" : "ChIJj6gQcRaHiV8RIk7P2Vis4wc",
         "types" : [ "park", "point_of_interest", "establishment" ]
      }
   ],
   "status" : "OK"
}

レスポンスの要素についても公式に詳細が載っていますので、そちらを参照ください。

Google Maps Geocoding APIの注意点

無料で使えるapiですが、以下のとおり制限があります。

  • 1秒間に最大10リクエストまで
  • 1日で最大2,500リクエストまで

※ビジネス向けに課金することで上記の制限を緩めることもできるようです。


今回はGoogle Maps Geocoding APIの使い方をまとめました。
比較的簡単に実装できますので、参考にしていただければと思います。

新しくなってさらに充実! より便利な機能が満載! Google マップ(ゴマブックス)

新しくなってさらに充実! より便利な機能が満載! Google マップ(ゴマブックス)

Google Maps JavaScript API V3 入門マスター

Google Maps JavaScript API V3 入門マスター