HTTP Geocoding Serviceについて

下書きのつもりが公開されてた。
かなり修正。



JavaScriptが使えない場面で、Google Static Maps APIを使うワケで。
でも郵便番号や住所から緯度経度を取得したい場合もある。


ちょっと調べた。


HTTP リクエスト経由のジオコーディング - Google Maps API - Google Code
HTTPリクエスト経由で取得できるらしい。

Maps API ジオコーダにアクセスするには、http://maps.google.com/maps/geo? に次のパラメータを持つ URI でリクエストを送信します。

  • q(必須) - ジオコーディングする住所。
  • key(必須) - API キー。
  • sensor(必須) - ジオコーディング リクエストが、場所センサーでデバイスから取得したものかどうかを示します。この値は、true か false のいずれかです。
  • output(必須) - 生成される出力の形式。xmlkmlcsvjson(デフォルト)を指定できます。
HTTP リクエスト経由のジオコーディング - Google Maps API - Google Code

任意項目は省略しました。


APIキーが必要なの?


とりあえず、Static Maps API v2の説明を確認する。
http://code.google.com/intl/en/apis/maps/documentation/staticmaps/
このページには特に記載されている様子はない(見逃した?)。


FAQを探してみると、こういう記述を見つけた。

When should I use an API geocoder class and when should I use the HTTP Geocoding Service?
Google provides two different techniques for performing geocoding. Each of the Maps APIs includes a class for performing geocoding and Google also provides a HTTP Geocoding Service.

FAQ - Google Maps API - Google Code

HTTPのサービスがあるよってことが書かれてる。
ちなみに、省略した残りの文章には、サーバ側でキャッシュしといて、Googleにリクエストする回数を抑えてね、みたいなことが書いてある。


で、リンクが貼ってあるHTTP Geocoding Serviceへ。
まず目に飛び込むふたつの注意書き。

New! The Google Geocoding Web Service has been significantly upgraded and enhanced. This document discusses this newest version of the Geocoding web service (V3). Note that the legacy Geocoding V2 Web Service has been deprecated. Users of that service should upgrade to this version.

HTTP Geocoding Service

Version 3に関する記述ってことか。

Note: The Google Geocoding Web Service no longer requires a Maps API key!
Google Maps API Premier customers must additionally sign their URLs using a new cryptographic key. See the Premier documentation for more information.

HTTP Geocoding Service

こちらはAPIキーが不要になったよってことか。

書式から。

http://maps.google.com/maps/api/geocode/output?parameters

http://code.google.com/intl/en/apis/maps/documentation/geocoding/index.html#GeocodingRequests

このoutputが曲者。

where output may be either of the following values:

http://code.google.com/intl/en/apis/maps/documentation/geocoding/index.html#GeocodingRequests

CSV形式がなくなっとる!


分解するのが面倒になった。


パラメタは以下の通り。引用するのが面倒なので、書き直します。


まずは必須項目。
address か latlng で示される場所ね。
addressなら住所や地名、郵便番号ってことで。郵便番号ならこんなふう?

address=460-0001

latlngはいつもどおりの緯度経度

latlng=40.714224,-73.961452


もうひとつの必須項目はsensorだけど、いいかげん使い道を探さないとなぁ。
とりあえずfasleを指定しておくか。

sensor=false

あとはそのうちでいいか。boudsとかregionとか、特に必要そうなものはないし。強いてあげればlanguageか。



試しに、名古屋市役所を郵便番号でリクエストしてみる。
http://maps.google.com/maps/api/geocode/xml?address=460-0001&sensor=false
レスポンスはこれ。

 <?xml version="1.0" encoding="UTF-8" ?> 
- <GeocodeResponse>
  <status>OK</status> 
- <result>
  <type>postal_code</type> 
  <formatted_address>〒460-0001</formatted_address> 
- <address_component>
  <long_name>460-0001</long_name> 
  <short_name>460-0001</short_name> 
  <type>postal_code</type> 
  </address_component>
- <address_component>
  <long_name>日本</long_name> 
  <short_name>JP</short_name> 
  <type>country</type> 
  <type>political</type> 
  </address_component>
- <geometry>
- <location>
  <lat>35.1802767</lat> 
  <lng>136.8954297</lng> 
  </location>
  <location_type>APPROXIMATE</location_type> 
- <viewport>
- <southwest>
  <lat>35.1771291</lat> 
  <lng>136.8922821</lng> 
  </southwest>
- <northeast>
  <lat>35.1834243</lat> 
  <lng>136.8985773</lng> 
  </northeast>
  </viewport>
  </geometry>
  </result>
  </GeocodeResponse>

やっぱりcsv形式のが簡単に操作できるよね?

JSON形式だとどうかというと…

{
  "status": "OK",
  "results": [ {
    "types": [ "postal_code" ],
    "formatted_address": "〒460-0001",
    "address_components": [ {
      "long_name": "460-0001",
      "short_name": "460-0001",
      "types": [ "postal_code" ]
    }, {
      "long_name": "日本",
      "short_name": "JP",
      "types": [ "country", "political" ]
    } ],
    "geometry": {
      "location": {
        "lat": 35.1802767,
        "lng": 136.8954297
      },
      "location_type": "APPROXIMATE",
      "viewport": {
        "southwest": {
          "lat": 35.1771291,
          "lng": 136.8922821
        },
        "northeast": {
          "lat": 35.1834243,
          "lng": 136.8985773
        }
      }
    }
  } ]
}

こっちならjson_decode()つかえるね。


HTTP Geocoding リクエスト用のURLを生成して、file_get_contents()で内容を取得。
そいつをjson_decode()して配列にするか。


つづく…?