從 Directions API 或 Distance Matrix API 遷移

本指南說明如何將使用 Directions API 或 Distance Matrix API 的應用程式遷移至 Routes API。如要進一步瞭解 Routes API,請參閱產品總覽

更新 REST API 端點

更新程式碼以使用新的 Routes API 端點

透過 Directions API

Directions API https://maps.googleapis.com/maps/api/directions/outputFormat?parameters
Routes API https://routes.googleapis.com/directions/v2:computeRoutes

透過 Distance Matrix API

Distance Matrix API https://maps.googleapis.com/maps/api/distancematrix/outputFormat?parameters
Routes API https://routes.googleapis.com/distanceMatrix/v2:computeRouteMatrix

將網址參數轉換為使用 HTTPS 要求主體

使用 Directions API 和 Distance Matrix API 時,您可以將設定屬性做為網址參數,傳送至 HTTP GET 要求。以 Directions API 為例:

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

使用 Routes API 時,您可以透過要求內文或標頭傳送參數,作為 HTTP POST 要求的一部分。如需範例,請參閱:

將編碼的折線轉換成路線控點,轉換為中繼路線控點

Directions API 中,您可以指定編碼折線線控點,以便符合網址最多 8192 個字元的網址控點。此方法不需要 Routes API 使用,因為路線控點可以在 REST 或 gRPC 要求主體中以「中繼」方式控點進行傳送。

將現有參數轉換為 Routes API 參數

下表列出在 Directions API 和 Distance Matrix API 中已重新命名或修改的參數,或是 Google Analytics (分析) 版本不支援的參數。如果您使用上述任何參數,請更新程式碼。

路徑或距離矩陣參數 Routes API 參數 Notes
alternatives computeAlternativeRoutes
arrival_time 無法使用「TRANSIT」模式,因此無法使用這項設定。
avoid routeModifiers
copyrights

未包含在回應中。如要向使用者顯示結果,請務必加入以下聲明:

Powered by Google, ©YEAR Google

例如:

Powered by Google, ©2022 Google

departure_time departureTime
distance distanceMeters 距離只能以公尺為單位。
duration_in_traffic 已從 Routes API 中移除,請使用 duration。詳情請參閱上方新 Routes API 的功能異動
language languageCode 僅支援 Compute 路徑。
mode travelMode

新增 TWO_WHEELER 支援。

TRANSIT」模式無法使用。

region regionCode

status 無法使用。針對 API 回報的錯誤,使用 HTTP 回應代碼。詳情請參閱「處理要求錯誤」。
traffic_model 不適用。
transit_mode 無法使用「TRANSIT」模式,因此無法使用這項設定。
transit_routing_preference 無法使用「TRANSIT」模式,因此無法使用這項設定。
units 不適用於路徑矩陣。
waypoints intermediates 已停止支援編碼折線
路線控點 optimize=true 不適用。