高度要求和回應

海拔高度要求

海拔高度 API 要求會建構為網址字串。這項 API 會傳回地表位置的海拔高度資料。您可以透過下列兩種方式指定位置資料:

  • 做為一或多個 locations 的集合。
  • 沿著 path 的一系列相連點。

這兩種方法都會使用經緯度座標來識別位置或路徑頂點。本文說明高程 API 網址的必要格式和可用參數。

海拔高度 API 會傳回單點查詢的資料,盡可能提供最高精確度。如果批次查詢涉及多個地點,且這些地點相距遙遠,則傳回的資料準確度可能會降低,因為系統會對資料進行某種程度的平滑化處理。

海拔高度 API 要求的格式如下:

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

其中 outputFormat 可以是下列任一值:

  • json (建議使用),表示輸出內容為 JavaScript 物件標記法 (JSON);或
  • xml,表示以 XML 格式輸出,並包裝在 <ElevationResponse> 節點中。

注意:網址必須經過適當編碼,才能有效使用,且所有網路服務的網址長度上限為 16,384 個字元。建構網址時,請注意這項限制。請注意,不同的瀏覽器、Proxy 和伺服器也可能有不同的網址字元限制。

使用 API 金鑰的要求必須透過 HTTPS 傳送。

要求參數

對 Elevation API 發出的要求會根據要求是針對離散位置還是排序路徑,使用不同的參數。如果是離散位置,海拔高度要求會傳回要求中傳遞的特定位置資料;如果是路徑,海拔高度要求則會沿著指定路徑取樣

依照所有網址的標準,參數會以 &amp; 字元分隔。以下列出參數及其可能的值。

所有要求

  • key - (必要) 應用程式的 API 金鑰。這個金鑰會識別您的應用程式,以利配額管理。瞭解如何取得金鑰

位置要求

  • locations (必要):定義要傳回海拔高度資料的地表位置。這個參數會採用單一位置 (以逗號分隔的 {latitude,longitude} 配對,例如「40.714728,-73.998672」),或是以陣列或編碼折線形式傳遞的多個經緯度配對。這個特定參數的點數上限為 512 點。詳情請參閱下方的「指定位置」。

路徑取樣要求

  • path (必要):定義要傳回海拔高度資料的地表路徑。這個參數會定義一組已排序的 {latitude,longitude} 組合,用來定義地球表面的路徑。這項參數必須與下文所述的 samples 參數搭配使用。這個特定參數的點數上限為 512 點。詳情請參閱下方的「指定路徑」。
  • samples (必要):指定要傳回海拔高度資料的路徑沿途取樣點數量。samples 參數會將指定的 path 分割為路徑沿途中一組已排序的等距點。

指定位置

位置要求會透過 locations 參數表示,指出針對以經緯度值傳遞的特定位置發出的海拔高度要求。

locations 參數可採用下列引數:

  • 單一座標:locations=40.714728,-73.998672
  • 以直立線 (「|」) 字元分隔的座標陣列:locations=40.714728,-73.998672|-34.397,150.644
  • 使用編碼折線演算法編碼的一組座標:locations=enc:gfo}EtohhU

經緯度座標字串是以逗號分隔的文字字串中的數字定義。舉例來說,「40.714728,-73.998672」是有效的 locations 值。緯度和經度值必須對應地球表面的有效位置。緯度值可介於 -9090 之間,經度值則可介於 -180180 之間。如果指定無效的緯度或經度值,系統會將要求視為錯誤要求而拒絕。

您可以在陣列或編碼折線中傳送最多 512 個座標,同時建構有效的網址。請注意,傳遞多個座標時,任何傳回資料的解析度準確性,可能會比只要求單一座標資料時更低。如果「locations」或「path」參數超過 512 個點或座標,系統會傳回 INVALID_REQUEST 回應。

指定路徑

使用 pathsamples 參數,表示要求路徑沿途特定間隔的海拔高度資料。與使用 locations 參數的位置要求一樣,path 參數會指定一組經緯度值。然而與位置要求不同的是,path 會指定一組已排序的端點。路徑要求不會只傳回端點的海拔高度資料,而是會沿著路徑長度取樣,並根據指定的 samples 數量 (包含端點) 傳回資料。

path 參數可採用下列任一引數:

  • 以逗號分隔的兩個以上座標文字字串陣列,並使用直立線 (「|」) 字元分隔:path=40.714728,-73.998672|-34.397,150.644
  • 使用編碼折線演算法編碼的座標:path=enc:gfo}EtohhUxD@bAxJmGF

經緯度座標字串是以逗號分隔的文字字串中的數字定義。舉例來說,「40.714728,-73.998672|-34.397, 150.644」是有效的 path 值。經緯度值必須對應地球表面上的有效位置。緯度值可介於 -9090 之間,經度值則可介於 -180180 之間。如果指定無效的緯度或經度值,系統會將要求視為錯誤要求而拒絕。

您可以在陣列或編碼折線中傳送最多 512 個座標,同時建構有效的網址。請注意,傳遞多個座標時,任何傳回資料的解析度準確性,可能會比只要求單一座標資料時更低。如果「locations」或「path」參數超過 512 個點或座標,系統會傳回 INVALID_REQUEST 回應。

海拔高度回應

對於每個有效的要求,海拔高度服務會以要求網址中指定的格式,傳回海拔高度回應。

ElevationResponse

FieldRequiredTypeDescription
required Array<ElevationResult> See ElevationResult for more information.
requiredElevationStatus See ElevationStatus for more information.
optionalstring

When the service returns a status code other than OK, there may be an additional error_message field within the response object. This field contains more detailed information about thereasons behind the given status code. This field is not always returned, and its content is subject to change.

ElevationStatus

Status codes returned by service.

  • OK indicating the API request was successful.
  • DATA_NOT_AVAILABLE indicating that there's no available data for the input locations.
  • INVALID_REQUEST indicating the API request was malformed.
  • OVER_DAILY_LIMIT indicating any of the following:
    • The API key is missing or invalid.
    • Billing has not been enabled on your account.
    • A self-imposed usage cap has been exceeded.
    • The provided method of payment is no longer valid (for example, a credit card has expired).
  • OVER_QUERY_LIMIT indicating the requestor has exceeded quota.
  • REQUEST_DENIED indicating the API did not complete the request.
  • UNKNOWN_ERROR indicating an unknown error.

如果狀態碼不是 OK,Elevation 回應物件中可能會有額外的 error_message 欄位。這個欄位包含有關指定狀態碼原因的詳細資訊。

回應包含 results 陣列,其中含有下列元素:

ElevationResult

FieldRequiredTypeDescription
requirednumber

The elevation of the location in meters.

requiredLatLngLiteral

A location element of the position for which elevation data is being computed. Note that for path requests, the set of location elements will contain the sampled points along the path.

See LatLngLiteral for more information.

optionalnumber

The value indicating the maximum distance between data points from which the elevation was interpolated, in meters. This property will be missing if the resolution is not known. Note that elevation data becomes more coarse (larger resolution values) when multiple points are passed. To obtain the most accurate elevation value for a point, it should be queried independently.

location 物件包含下列元素:

LatLngLiteral

An object describing a specific location with Latitude and Longitude in decimal degrees.

FieldRequiredTypeDescription
requirednumber

Latitude in decimal degrees

requirednumber

Longitude in decimal degrees

位置海拔高度範例

以下範例以 JSON 格式要求科羅拉多州丹佛市 (又稱「一英里高城」) 的海拔高度:

網址

https://maps.googleapis.com/maps/api/elevation/json
  ?locations=39.7391536%2C-104.9847034
  &key=YOUR_API_KEY

cURL

curl -L -X GET 'https://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536%2C-104.9847034&key=YOUR_API_KEY'

JSON

{
  "results":
    [
      {
        "elevation": 1608.637939453125,
        "location": { "lat": 39.7391536, "lng": -104.9847034 },
        "resolution": 4.771975994110107,
      },
    ],
  "status": "OK",
}

XML

<ElevationResponse>
 <status>OK</status>
 <result>
  <location>
   <lat>39.7391536</lat>
   <lng>-104.9847034</lng>
  </location>
  <elevation>1608.6379395</elevation>
  <resolution>4.7719760</resolution>
 </result>
</ElevationResponse>

以下範例顯示多個回應 (分別是科羅拉多州丹佛和加州死亡谷)。

這項要求示範如何使用 JSON output 旗標:

網址

https://maps.googleapis.com/maps/api/elevation/json
  ?locations=39.7391536%2C-104.9847034%7C36.455556%2C-116.866667
  &key=YOUR_API_KEY

cURL

curl -L -X GET 'https://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536%2C-104.9847034%7C36.455556%2C-116.866667&key=YOUR_API_KEY'

這項要求示範如何使用 XML output 標記:

https://maps.googleapis.com/maps/api/elevation/xml?locations=39.7391536,-104.9847034|36.455556,-116.866667&key=YOUR_API_KEY

選取下方分頁標籤,查看 JSON 和 XML 回應範例。

JSON

{
  "results":
    [
      {
        "elevation": 1608.637939453125,
        "location": { "lat": 39.7391536, "lng": -104.9847034 },
        "resolution": 4.771975994110107,
      },
      {
        "elevation": -52.79492568969727,
        "location": { "lat": 36.455556, "lng": -116.866667 },
        "resolution": 19.08790397644043,
      },
    ],
  "status": "OK",
}

XML

<ElevationResponse>
 <status>OK</status>
 <result>
  <location>
   <lat>39.7391536</lat>
   <lng>-104.9847034</lng>
  </location>
  <elevation>1608.6379395</elevation>
  <resolution>4.7719760</resolution>
 </result>
 <result>
  <location>
   <lat>36.4555560</lat>
   <lng>-116.8666670</lng>
  </location>
  <elevation>-52.7949257</elevation>
  <resolution>19.0879040</resolution>
 </result>
</ElevationResponse>

以下範例要求從加州惠特尼山到加州惡水盆地 (美國本土最高和最低點) 的直線path海拔高度資料。我們要求提供三個點 samples,因此會包含兩個端點和中途點。

網址

https://maps.googleapis.com/maps/api/elevation/json
  ?path=36.578581%2C-118.291994%7C36.23998%2C-116.83171
  &samples=3
  &key=YOUR_API_KEY

cURL

curl -L -X GET 'https://maps.googleapis.com/maps/api/elevation/json?path=36.578581%2C-118.291994%7C36.23998%2C-116.83171&samples=3&key=YOUR_API_KEY'

JSON

{
  "results":
    [
      {
        "elevation": 4411.94189453125,
        "location": { "lat": 36.578581, "lng": -118.291994 },
        "resolution": 19.08790397644043,
      },
      {
        "elevation": 1372.8359375,
        "location": { "lat": 36.41150289067028, "lng": -117.5602607523847 },
        "resolution": 9.543951988220215,
      },
      {
        "elevation": -84.51690673828125,
        "location": { "lat": 36.23998, "lng": -116.83171 },
        "resolution": 9.543951988220215,
      },
    ],
  "status": "OK",
}

XML

<ElevationResponse>
 <status>OK</status>
 <result>
  <location>
   <lat>36.5785810</lat>
   <lng>-118.2919940</lng>
  </location>
  <elevation>4411.9418945</elevation>
  <resolution>19.0879040</resolution>
 </result>
 <result>
  <location>
   <lat>36.4115029</lat>
   <lng>-117.5602608</lng>
  </location>
  <elevation>1372.8359375</elevation>
  <resolution>9.5439520</resolution>
 </result>
 <result>
  <location>
   <lat>36.2399800</lat>
   <lng>-116.8317100</lng>
  </location>
  <elevation>-84.5169067</elevation>
  <resolution>9.5439520</resolution>
 </result>
</ElevationResponse>