Google geocoding api
提供: sha.ngri.la
Google Geocoding APIの使い方のメモです。
http://maps.googleapis.com/maps/api/geocode/output?parameters
output
には、xml
又はjson
が入ります。
parameters
には、address
などが入ります。
具体的には、例えば「東京タワー」の緯度経度を調べたいなら、
http://maps.googleapis.com/maps/api/geocode/json?address=東京タワー&sensor=false
このアドレスにブラウザでアクセスすると、
{ "results" : [ { "address_components" : [ { "long_name" : "東京タワー", "short_name" : "東京タワー", "types" : [ "point_of_interest", "establishment" ] }, { "long_name" : "8", "short_name" : "8", "types" : [ "sublocality_level_4", "sublocality", "political" ] }, { "long_name" : "2", "short_name" : "2", "types" : [ "sublocality_level_3", "sublocality", "political" ] }, { "long_name" : "4丁目", "short_name" : "4丁目", "types" : [ "sublocality_level_2", "sublocality", "political" ] }, { "long_name" : "芝公園", "short_name" : "芝公園", "types" : [ "sublocality_level_1", "sublocality", "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" : "105-0011", "short_name" : "105-0011", "types" : [ "postal_code" ] } ], "formatted_address" : "日本, 〒105-0011 東京都港区芝公園4丁目2−8 東京タワー", "geometry" : { "location" : { "lat" : 35.6585805, "lng" : 139.7454329 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 35.6599294802915, "lng" : 139.7467818802915 }, "southwest" : { "lat" : 35.6572315197085, "lng" : 139.7440839197085 } } }, "place_id" : "ChIJCewJkL2LGGAR3Qmk0vCTGkg", "types" : [ "point_of_interest", "establishment" ] } ], "status" : "OK" }
「東京都港区芝公園4丁目2−8」を使って
http://maps.googleapis.com/maps/api/geocode/json?address=東京都港区芝公園4丁目2−8&sensor=false
にアクセスすると、
{ "results" : [ { "address_components" : [ { "long_name" : "8", "short_name" : "8", "types" : [ "sublocality_level_4", "sublocality", "political" ] }, { "long_name" : "2", "short_name" : "2", "types" : [ "sublocality_level_3", "sublocality", "political" ] }, { "long_name" : "4丁目", "short_name" : "4丁目", "types" : [ "sublocality_level_2", "sublocality", "political" ] }, { "long_name" : "芝公園", "short_name" : "芝公園", "types" : [ "sublocality_level_1", "sublocality", "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" : "105-0011", "short_name" : "105-0011", "types" : [ "postal_code" ] } ], "formatted_address" : "日本, 〒105-0011 東京都港区芝公園4丁目2−8", "geometry" : { "location" : { "lat" : 35.6585928, "lng" : 139.7454636 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 35.6599417802915, "lng" : 139.7468125802915 }, "southwest" : { "lat" : 35.6572438197085, "lng" : 139.7441146197085 } } }, "place_id" : "ChIJ2el4kL2LGGARRVFaQHhRDUw", "types" : [ "sublocality_level_4", "sublocality", "political" ] } ], "status" : "OK" }
と表示されます。
このjsonで出力された内容を利用するために、rubyでスクリプトを書いてみました。
#coding:utf-8 require 'open-uri' require 'uri' require 'json' geocodeurl = "https://maps.googleapis.com/maps/api/geocode/json?address=" geocodeend = "&sensor=false&language=ja" address = "東京都港区芝公園4丁目2−8" url = geocodeurl + address + geocodeend puts url url = URI.escape(url) json = open(url) json_text ="" json.each do |line| json_text = json_text + line end puts json_text json_parsed = JSON.parse(json_text) puts json_parsed puts json_parsed["results"][0]["formatted_address"] add_com = "" add_com = json_parsed["results"][0]["address_components"] add_com.each do |address| puts address["types"][0] + ":" + address["long_name"] end
適当な場所に、geocode.rb
と名前をつけて保存して、ruby geocode.rb
で実行すると次のように表示されるはずです。
https://maps.googleapis.com/maps/api/geocode/json?address=東京都港区芝公園4丁目2−8&sensor=false&language=ja { "results" : [ { "address_components" : [ { "long_name" : "8", "short_name" : "8", "types" : [ "sublocality_level_4", "sublocality", "political" ] }, { "long_name" : "2", "short_name" : "2", "types" : [ "sublocality_level_3", "sublocality", "political" ] }, { "long_name" : "4丁目", "short_name" : "4丁目", "types" : [ "sublocality_level_2", "sublocality", "political" ] }, { "long_name" : "芝公園", "short_name" : "芝公園", "types" : [ "sublocality_level_1", "sublocality", "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" : "105-0011", "short_name" : "105-0011", "types" : [ "postal_code" ] } ], "formatted_address" : "日本, 〒105-0011 東京都港区芝公園4丁目2−8", "geometry" : { "location" : { "lat" : 35.6585928, "lng" : 139.7454636 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 35.6599417802915, "lng" : 139.7468125802915 }, "southwest" : { "lat" : 35.6572438197085, "lng" : 139.7441146197085 } } }, "place_id" : "ChIJ2el4kL2LGGARRVFaQHhRDUw", "types" : [ "sublocality_level_4", "sublocality", "political" ] } ], "status" : "OK" } {"results"=>[{"address_components"=>[{"long_name"=>"8", "short_name"=>"8", "types"=>["sublocality_level_4", "sublocality", "political"]}, {"long_name"=>"2", "short_name"=>"2", "types"=>["sublocality_level_3", "sublocality", "political"]}, {"long_name"=>"4丁目", "short_name"=>"4丁目", "types"=>["sublocality_level_2", "sublocality", "political"]}, {"long_name"=>"芝公園", "short_name"=>"芝公園", "types"=>["sublocality_level_1", "sublocality", "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"=>"105-0011", "short_name"=>"105-0011", "types"=>["postal_code"]}], "formatted_address"=>"日本, 〒105-0011 東京都港区芝公園4丁目2−8", "geometry"=>{"location"=>{"lat"=>35.6585928, "lng"=>139.7454636}, "location_type"=>"ROOFTOP", "viewport"=>{"northeast"=>{"lat"=>35.6599417802915, "lng"=>139.7468125802915}, "southwest"=>{"lat"=>35.6572438197085, "lng"=>139.7441146197085}}}, "place_id"=>"ChIJ2el4kL2LGGARRVFaQHhRDUw", "types"=>["sublocality_level_4", "sublocality", "political"]}], "status"=>"OK"} 日本, 〒105-0011 東京都港区芝公園4丁目2−8 sublocality_level_4:8 sublocality_level_3:2 sublocality_level_2:4丁目 sublocality_level_1:芝公園 locality:港区 administrative_area_level_1:東京都 country:日本 postal_code:105-0011
address_componentsのtypes
types | 内容 |
---|---|
premise | ビル名、建物の名前等 |
sublocality_level_5 | 番地等 |
sublocality_level_4 | 番地等 |
sublocality_level_3 | 番地等 |
sublocality_level_2 | 地名、町名 |
sublocality_level_1 | 地名、町名 |
ward | 政令指定都市の区 |
locality | 市町村、東京都の区 |
colloquial_area | 郡 |
administrative_area_level_1 | 都道府県 |
country | 国名 |
postal_code | 郵便番号 |
formatted_address | フォーマットされた住所 |