ジオコーディングでは、住所から緯度・経度の地理座標を取得できます。またはその逆に、緯度・経度から住所を取得する (逆ジオコーディング (Reverse Geocoding) ) ことも可能です。
ジオコーディングのキャッシュ
ジオコーディングは時間とリソースを消費するタスクです。可能な場合は、既知の住所をあらかじめ(ここで説明する Geocoding API や他のジオコーディング サービスを使用して)ジオコーディングし、独自に設計した一時キャッシュにその結果を保存するようにしてください。
対象読者 - Google Geocoding API - Google Maps API | Google Developers
ISO-3166の国コードなども有効なようです。
指定のURLへパラメータを付加してリクエストし、レスポンスをJSONで取得します。
https://maps.google.com/maps/api/geocode/json
?address=住所
&sensor=false
緯度と経度から、住所の取得もできます。
https://maps.google.com/maps/api/geocode/json
?latlng=緯度,経度
&sensor=false
| パラメータ | 説明 | 指定方法 | 必須 |
|---|---|---|---|
| address | 取得対象の住所 | テキスト | ○※1 |
| latlng | 緯度と経度 | テキスト | |
| sensor | 位置センサー搭載のデバイスからのリクエストかどうか | true / false | ○ |
| bounds | ビューポートの境界ボックス
境界ボックスの南西と北東の角の緯度・経度で指定 (例34.172684,-118.604794|34.236144,-118.500938)
|
テキスト | |
| region | 地域コード
ccTLDの2文字の値 |
地域コード | |
| language | 結果取得時の言語 | 言語コード |
| フィールド | 説明 | ||
|---|---|---|---|
| types[] | 取得結果の対象物の種類 | ||
| formatted_address | 郵便のあて先 | ||
| address_components[] | types[] | 住所の種類 | |
| long_name | 住所の名前 | ||
| short_name | 住所の略称 | ||
| geometry | location | lat | 緯度 |
| lng | 経度 | ||
| location_type | 取得結果の精度 | ||
| viewport | southwest | ビューポートの南西の座標 | |
| northeast | ビューポートの北東の座標 | ||
| bounds | southwest | 境界ボックスの南西の座標 | |
| northeast | 境界ボックスの北東の座標 | ||
{
"status": "OK",
"results": [ {
"types": [ "administrative_area_level_1", "political" ],
"formatted_address": "日本, 東京都",
"address_components": [ {
"long_name": "東京都",
"short_name": "東京都",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "日本",
"short_name": "JP",
"types": [ "country", "political" ]
} ],
"geometry": {
"location": {
"lat": 35.6894875,
"lng": 139.6917064
},
"location_type": "APPROXIMATE",
"viewport": {
"southwest": {
"lat": 35.5011900,
"lng": 138.9427580
},
"northeast": {
"lat": 35.8986440,
"lng": 139.9198530
}
},
"bounds": {
"southwest": {
"lat": 35.5011900,
"lng": 138.9427580
},
"northeast": {
"lat": 35.8986440,
"lng": 139.9198530
}
}
}
} ]
}
{
"results" : [
{
"address_components" : [
{
"long_name" : "1",
"short_name" : "1",
"types" : [ "sublocality_level_4", "sublocality", "political" ]
},
{
"long_name" : "8",
"short_name" : "8",
"types" : [ "sublocality_level_3", "sublocality", "political" ]
},
{
"long_name" : "2丁目",
"short_name" : "2丁目",
"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" ]
}
],
"formatted_address" : "日本, 東京都新宿区西新宿2丁目8−1",
"geometry" : {
"location" : {
"lat" : 35.68918480,
"lng" : 139.69164810
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.69053378029150,
"lng" : 139.6929970802915
},
"southwest" : {
"lat" : 35.68783581970850,
"lng" : 139.6902991197085
}
}
},
"types" : [ "sublocality_level_4", "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "8",
"short_name" : "8",
"types" : [ "sublocality_level_3", "sublocality", "political" ]
},
{
"long_name" : "2丁目",
"short_name" : "2丁目",
"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" ]
}
],
"formatted_address" : "日本, 東京都新宿区西新宿2丁目8",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.69049380,
"lng" : 139.69252850
},
"southwest" : {
"lat" : 35.68858650,
"lng" : 139.69070770
}
},
"location" : {
"lat" : 35.68955420,
"lng" : 139.69180910
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.69088913029150,
"lng" : 139.6929670802915
},
"southwest" : {
"lat" : 35.68819116970850,
"lng" : 139.6902691197085
}
}
},
"types" : [ "sublocality_level_3", "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "163-8001",
"short_name" : "163-8001",
"types" : [ "postal_code" ]
},
{
"long_name" : "日本",
"short_name" : "JP",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "〒163-8001, 日本",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.69083270000001,
"lng" : 139.69241360
},
"southwest" : {
"lat" : 35.68687870,
"lng" : 139.69113740
}
},
"location" : {
"lat" : 35.68950140,
"lng" : 139.69169250
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.69083270000001,
"lng" : 139.6931244802915
},
"southwest" : {
"lat" : 35.68687870,
"lng" : 139.6904265197085
}
}
},
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "2丁目",
"short_name" : "2丁目",
"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" ]
}
],
"formatted_address" : "日本, 東京都新宿区西新宿2丁目",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.69250290,
"lng" : 139.69565330
},
"southwest" : {
"lat" : 35.68610049999999,
"lng" : 139.68723340
}
},
"location" : {
"lat" : 35.69134850,
"lng" : 139.69358670
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.69250290,
"lng" : 139.69565330
},
"southwest" : {
"lat" : 35.68610049999999,
"lng" : 139.68723340
}
}
},
"types" : [ "sublocality_level_2", "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "都庁前駅",
"short_name" : "都庁前駅",
"types" : [ "train_station", "transit_station", "establishment" ]
},
{
"long_name" : "東京都",
"short_name" : "東京都",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "日本",
"short_name" : "JP",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "日本, 都庁前駅(東京)",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.6953840,
"lng" : 139.6974090
},
"southwest" : {
"lat" : 35.6846380,
"lng" : 139.687280
}
},
"location" : {
"lat" : 35.6905510,
"lng" : 139.692570
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.6953840,
"lng" : 139.6974090
},
"southwest" : {
"lat" : 35.6846380,
"lng" : 139.687280
}
}
},
"types" : [ "train_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"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" ]
}
],
"formatted_address" : "日本, 東京都新宿区西新宿",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.69796530,
"lng" : 139.70015830
},
"southwest" : {
"lat" : 35.68175390,
"lng" : 139.68317130
}
},
"location" : {
"lat" : 35.69247590,
"lng" : 139.68550410
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.69796530,
"lng" : 139.70015830
},
"southwest" : {
"lat" : 35.68175390,
"lng" : 139.68317130
}
}
},
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "新宿駅",
"short_name" : "新宿駅",
"types" : [ "train_station", "transit_station", "establishment" ]
},
{
"long_name" : "東京都",
"short_name" : "東京都",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "日本",
"short_name" : "JP",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "日本, 新宿駅(東京)",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.7005790,
"lng" : 139.7124960
},
"southwest" : {
"lat" : 35.6812220,
"lng" : 139.6878250
}
},
"location" : {
"lat" : 35.6909210,
"lng" : 139.7002580
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.7005790,
"lng" : 139.7124960
},
"southwest" : {
"lat" : 35.6812220,
"lng" : 139.6878250
}
}
},
"types" : [ "train_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "160-0022",
"short_name" : "160-0022",
"types" : [ "postal_code" ]
},
{
"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" ]
}
],
"formatted_address" : "〒160-0022, 日本",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.70604260,
"lng" : 139.71922230
},
"southwest" : {
"lat" : 35.68222330,
"lng" : 139.69113740
}
},
"location" : {
"lat" : 35.69110170,
"lng" : 139.7067630
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.70604260,
"lng" : 139.71922230
},
"southwest" : {
"lat" : 35.68222330,
"lng" : 139.69624280
}
}
},
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "160-0023",
"short_name" : "160-0023",
"types" : [ "postal_code" ]
},
{
"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" ]
}
],
"formatted_address" : "〒160-0023, 日本",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.71554820,
"lng" : 139.71156140
},
"southwest" : {
"lat" : 35.68138590,
"lng" : 139.68220410
}
},
"location" : {
"lat" : 35.69247590,
"lng" : 139.68550410
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.7002790,
"lng" : 139.71156140
},
"southwest" : {
"lat" : 35.68138590,
"lng" : 139.68220410
}
}
},
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"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" ]
}
],
"formatted_address" : "日本, 東京都新宿区",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.72989390,
"lng" : 139.74516150
},
"southwest" : {
"lat" : 35.67315150,
"lng" : 139.67324880
}
},
"location" : {
"lat" : 35.69384010,
"lng" : 139.70354940
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.72989390,
"lng" : 139.74516150
},
"southwest" : {
"lat" : 35.67315150,
"lng" : 139.67324880
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "東京都",
"short_name" : "東京都",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "日本",
"short_name" : "JP",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "日本, 東京都",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.89864410,
"lng" : 142.24878780
},
"southwest" : {
"lat" : 24.22423430,
"lng" : 138.9427580
}
},
"location" : {
"lat" : 35.68948750,
"lng" : 139.69170640
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.8178130,
"lng" : 139.9102020
},
"southwest" : {
"lat" : 35.5288730,
"lng" : 139.5105740
}
}
},
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "日本",
"short_name" : "JP",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "日本",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 45.55397989999999,
"lng" : 148.89401860
},
"southwest" : {
"lat" : 24.04599860,
"lng" : 122.93378460
}
},
"location" : {
"lat" : 36.2048240,
"lng" : 138.2529240
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 45.55397989999999,
"lng" : 148.89367270
},
"southwest" : {
"lat" : 24.04810930,
"lng" : 122.93378460
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
JavaScriptのオブジェクトを通して、結果を取得します。
geocode(
request:GeocoderRequest,
callback:function( Array.<GeocoderResult>, GeocoderStatus )
)
requestには、GeocoderRequestの4つのプロパティのいずれかを指定します。
| プロパティ | 型 | 説明 | 必須 |
|---|---|---|---|
| address | string | 住所を表す文字列 | ○※1 |
| location | LatLng | 緯度・経度を表すLatLngオブジェクト | ○※1 |
| bounds | LatLngBounds | 領域を表すLatLngBoundsオブジェクト | |
| region | string | 国コードのトップレベルドメインを表す文字列 |
addressを指定するとジオコーディングを、locationならば逆ジオコーディングを行います。boundsとregionは検索を偏向させるために使用され、それだけでは検索できません。
このGeocoderRequestはただのオブジェクトリテラルで、{ address: 'tokyo' }のようにオブジェクトを作成することを求められているにすぎません。
| プロパティ | 型 | 説明 |
|---|---|---|
| formatted_address | string | 位置を表す人間が判読可能な文字列 |
| types | string[] | |
| address_components | GeocoderAddressComponent[] | 住所の個々の要素 |
| geometry | GeocoderGeometry | GeocoderResultの情報 |
GeocoderResultを構成する住所の1つ
| プロパティ | 型 | 説明 |
|---|---|---|
| long_name | string | 住所を表すテキスト |
| short_name | string | 住所を簡単に表すテキスト |
| types | string[] | 住所の種類 |
GeocoderResultの情報
| プロパティ | 型 | 説明 |
|---|---|---|
| bounds | LatLngBounds | 結果の正確な領域 |
| viewport | LatLngBounds | 結果を表示するのに適した領域 |
| location | LatLng | 結果の緯度・経度 |
| location_type | GeocoderLocationType | 結果の位置の種類 |
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
geocoder.geocode(
{ address: 'tokyo' },
function( results, status )
{
if( status == google.maps.GeocoderStatus.OK )
{
alert( results[ 0 ].geometry.location );
}
else
{
alert( 'Faild:' + status );
}
} );
</script>
| 種類 | 説明 |
|---|---|
| street_address | 正確な住所を示す |
| route | 名前が付いているルート (たとえば「US 101」) を示す |
| intersection | 主要な交差点 (通常は2本の大通りの交差点) を示す |
| political | 政治的エンティティを示す。通常、このタイプは一部の民政のポリゴンを示す |
| country | 国政エンティティを示す。一般的に、ジオコーダーから返されるタイプの中で最上位に位置付けられる |
| administrative_area_level_1 | 国レベルの下の第1次行政エンティティを示す。米国内の場合、この行政レベルは州。この行政レベルがない国もある |
| administrative_area_level_2 | 国レベルの下の第2次行政エンティティを示す。米国内の場合、この行政レベルは郡。この行政レベルがない国もある |
| administrative_area_level_3 | 国レベルの下の第3次行政エンティティを示す。このタイプは、小規模な行政区分を示す。この行政レベルがない国もある |
| colloquial_area | 一般的に使用されている通称を示す |
| locality | 都市や町などの政治的エンティティの地域を示す |
| sublocality | 地域の下の第1次行政エンティティを示す |
| neighborhood | 名前が付いている周辺地区を示す |
| premise | 名前のある場所を示す。通常は共通の名前を持つ建物や建物の集合体 |
| subpremise | 指定された場所の下の第1次エンティティを示す。通常は、共通の名前を持つ建物の集合体内の建物1棟 |
| postal_code | 対象の国内で郵便物の宛先として使用される郵便番号を示す |
| natural_feature | 特徴的な地勢を示す |
| airport | 空港を示す |
| park | 名前のある公園を示す |
| point_of_interest | 名前のあるスポットを示す。通常、このようなスポットには「エンパイアステートビル」や「自由の女神」など、他のカテゴリへの分類が難しい、地域の著名な実在物が該当する |
| 種類 | 説明 |
|---|---|
| post_box | 特定の私書箱を示す |
| street_number | 正確な番地を示す |
| floor | 建物の住所の階数を示す |
| room | 建物の住所の部屋を示す |