從 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,配合大型 網址所包含的路線控點數目上限為 16384 個字元。此功能不是 ,因為路點可傳輸至 經緯度座標的 REST 或 gRPC 要求主體。例如 請參閱 HTTP 範例 ,或是參考「計算路徑矩陣」文件 定義中繼路線點 ,請參閱指定中繼路線控點文件。

轉換參數

下表列出了 具備 Directions API 和 Distance Matrix API 重新命名或修改,或是 Google Analytics 不支援的參數 版本。如果您使用其中任何參數,請更新程式碼。

要求參數轉換

路線或距離矩陣參數 Routes API 參數 附註
alternatives computeAlternativeRoutes
arrival_time arrivalTime 僅限 TRANSIT 模式使用,不得相同 因為 departureTime
avoid routeModifiers
departure_time departureTime 無法與「arrivalTime」同時使用。
language languageCode 僅支援 Compute 路徑。
mode travelMode

新增對 TWO_WHEELERTRANSIT

region regionCode
traffic_model trafficModel 瞭解詳情
transit_mode "travelMode": "TRANSIT" 在 Directions API 中, 相同交通方式的行程路段 (例如「步行」或 大眾運輸) 都視為一個步驟,而 是多步驟的交通方式相對地 Routes API,步驟一致都是單一導覽指示 各種旅遊類型因此,每個導覽指示都是一個步驟。 如果在單一交通模式中有多個步驟,Routes API 提供 中繼資料,其中包含該交通方式的步驟摘要, `stepsOverview`.如要要求這些中繼資料,請使用 `routes.legs.stepsOverview` 欄位遮罩。 瞭解詳情
transit_routing_preference transitRoutingPreference 瞭解詳情
units 不適用於路徑矩陣。
waypoints intermediates 移除對編碼折線的支援。
optimize=true 代表路線控點 "optimizeWaypointOrder": "true" 瞭解詳情

回應參數轉換

路線或距離矩陣參數 Routes API 參數 附註
copyrights

未包含在回覆中。必須加入下列資訊 陳述式:

Powered by Google, ©YEAR Google

例如:

Powered by Google, ©2022 Google

distance distanceMeters 距離只能以公尺為單位。
duration_in_traffic duration 已從 Routes API 中移除,請使用 duration
status 不適用。針對以下項目使用 HTTP 回應代碼: API 回報的錯誤請參閱「處理要求錯誤」。 瞭解詳情
geocoded_waypoints geocoding_results 僅支援 Compute 路徑。僅包含指定為地址的路線控點的地理編碼回應資訊。
bounds viewport
legs.end_address 不適用。
legs.start_address 不適用。
overview_polyline 折線
summary description
waypoint_order optimizedIntermediateWaypointIndex