LoginSignup
1
2

More than 3 years have passed since last update.

Google Map API 使ってみた

Posted at

はじめに

住所から緯度経度変換がしたい!
そうだ、Google Map APIを使おう!

使用するAPI

outputFormatとparametersは任意の値

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

リクエスト

outputFormat

jsonかxmlを指定できる

parameters(必須)

address(アドレス)とkey(APIキー)
オプションは割愛

レスポンス

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4224764,
               "lng" : -122.0842499
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4238253802915,
                  "lng" : -122.0829009197085
               },
               "southwest" : {
                  "lat" : 37.4211274197085,
                  "lng" : -122.0855988802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "plus_code": {
            "compound_code": "CWC8+W5 Mountain View, California, United States",
            "global_code": "849VCWC8+W5"
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

status

リクエストのステータスが含まれ、APIが機能していない理由を追跡するのに役立つデバッグ情報が含まれる

status 意味
OK 住所は正常に解析され、少なくとも1つのジオコードが返却
ZERO_RESULTS ジオコードは成功したが結果が返却されない(ジオコーダーに存在しない住所が渡された場合に発生することがある
OVER_DAILY_LIMIT APIキーが正しくない、アカウントが使えない、使用量の上限が超えている、お支払い方法が無効になっている
OVER_QUERY_LIMIT 割当量が超えている
REQUEST_DENIED リクエストが拒否された
INVALID_REQUEST リクエストパラメータがおかしい
UNKNOWN_ERROR サーバーエラーでリクエストが処理できないかった(再試行すると成功する場合がある)

ステータスがOK以外の時はerror_messageが返却される場合がある

Results

types

シカゴの場合、都市を意味する「locality」を返却する
(政治的実体である「political」も返却する)

formatted_address

人間が読める住所

address_components

適用可能な個別のコンポーネント

status 意味
types コンポーネントのタイプ(typesの種類
long_name コンポーネントの全文説明
short_name コンポーネントの略称
postcode_localities

郵便番号に含まれるすべての地域を示す

geometry
status 意味
location ジオコーディングされた緯度と経度の値
location_type 指定された場所に関する追加データ
viewport 返された結果を表示するための推奨ビューポート
bounds 返された結果を完全に含むことができる境界ボックス
plus_code

番地の代わりに使用できる

partial_match

ジオコーダーが要求された住所の一部と一致することはできたものの、元の要求と完全に一致するものを返さなかったことを示し、スペルミスや不完全な住所の元のリクエストを調べることが推奨されている

place_id

Google APIで使用できる一意の識別子

まとめ

存在しない住所を入力しても部分一致して返してくれたり、判別できなくても200で返却されるので、実際に使う際は返却値によって場合わけが必要そう。

1
2
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
1
2