Google Maps Geocoding API 是提供地址的地理編碼及反向地理編碼的服務。
此服務也以用戶端 Google Maps JavaScript API 之一部分的方式提供,或是搭配 Java Client、Python Client、Go Client 與 Node.js Client for Google Maps Services 針對伺服器端使用。
地理編碼是將地址(例如街道地址)轉換為地理座標(緯度與經度)的程序,您可以用來在地圖上放置標記或定位地圖。
反向地理編碼是將地理座標轉換成人類看得懂之地址的程序。Google Maps Geocoding API 的反向地理編碼服務也可以讓您找到指定地點 ID 的地址。
範例要求與回應
您可以透過 HTTP 介面存取 Google Maps Geocoding API。以下是地理編碼和反向地理編碼要求的範例。
地理編碼要求與回應(查詢緯度/經度)
下列範例要求查詢「1600 Amphitheatre Parkway, Mountain View, CA」的緯度和經度,並指定以 JSON 格式輸出:
https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY
測試的方法是在網頁瀏覽器輸入網址(請務必使用您的實際 API 金鑰取代 ‘YOUR_API_KEY’)。回應包含該地址的緯度和經度。
查看開發人員指南,瞭解建置地理編碼要求網址與可用參數的詳細資訊以及瞭解回應。
下面是 JSON 格式的地理編碼範例回應:
{
"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",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
反向地理編碼要求與回應(查詢地址)
下列範例要求查詢美國紐約州布魯克林內,特定緯度/經度所對應的地址。指定以 JSON 格式輸出。
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY
測試的方法是在網頁瀏覽器輸入網址(請務必使用您的實際 API 金鑰取代 ‘YOUR_API_KEY’)。回應包含針對該緯度、經度位置的一般人看得懂的地址。
查看開發人員指南,瞭解建置反向地理編碼要求網址與可用參數的詳細資訊以及瞭解回應。
下面是 JSON 格式的反向地理編碼範例回應:
{
"results" : [
{
"address_components" : [
{
"long_name" : "277",
"short_name" : "277",
"types" : [ "street_number" ]
},
{
"long_name" : "Bedford Avenue",
"short_name" : "Bedford Ave",
"types" : [ "route" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Kings",
"short_name" : "Kings",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "277 Bedford Avenue, Brooklyn, NY 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.714232,
"lng" : -73.9612889
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.7155809802915,
"lng" : -73.9599399197085
},
"southwest" : {
"lat" : 40.7128830197085,
"lng" : -73.96263788029151
}
}
},
"place_id" : "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
"types" : [ "street_address" ]
},
... Additional results truncated in this example[] ...
],
"status" : "OK"
}
開始使用我們的用戶端程式庫進行編碼
用戶端程式庫透過提供常見工作之簡易、原生的實作(例如驗證、要求節流與自動重試),使利用 Google 地圖 Web 服務 API 進行開發的工作變得更輕鬆。Google Maps Geocoding API 可從 Java Client、Python Client、Go Client 與 Node.js Client for Google Maps Services 取得。
驗證、配額與政策
啟用 API 及取得 API 金鑰
若要使用 Google Maps Geocoding API,您必須先在 Google API Console 中啟用 API 並取得正確的驗證憑證。您必須在每個要求中提供 API 金鑰 (或如果您有進階方案,則必須提供用戶端編號)。
按一下下面的按鈕以依序操作整個程序,您將能夠:
- 建立或選取專案
- 啟用 API
- 取得 API 金鑰
配額
查看使用限制頁面,了解為 Google Maps Geocoding API 設定之配額的詳細資料。
政策
使用 Google Maps Geocoding API 時必須符合 API 政策。
深入瞭解
使用 Geocoding API 還可以執行其他工作。請參閱 Geocoding API 開發人員指南,取得其他示範、範例、可用參數、狀態碼、錯誤訊息等其他詳細資料。
