查看路徑矩陣回應
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
路線矩陣要求會將起點和目的地路線點陣列做為輸入內容。接著,Routes API 會計算從每個起點路線控點到每個目的地路線控點的路線。舉例來說,如果您指定兩個起點路線控點和兩個目的地路線控點,API 就會計算四個獨立的路線。
API 回應包含陣列,其中每個陣列元素都對應至從一個起點路線控點到一個目的地路線控點的路線。因此,如果您指定兩個起點路線控點和兩個目的地路線控點,回應陣列就會包含四個元素,每個計算路線一個。
您可以使用回應,為客戶提供不同的路線選項,然後讓使用者根據需求選取合適的路線。
注意: 路線矩陣不會傳回詳細的路線資訊,例如路線的路段、步驟和多邊形。如要取得詳細路線資訊,請將單一起點和單一目的地傳遞至 Compute Routes 。 關於欄位遮罩
呼叫方法來計算路線時,您必須指定欄位遮罩,定義要在回應中傳回哪些欄位。系統不會提供傳回欄位的預設清單。如果省略這個清單,方法會傳回錯誤。
本文件中的範例會顯示整個回應物件,但不會考量欄位遮罩。在實際工作環境中,回應只會包含您在欄位遮罩中明確指定的欄位。
詳情請參閱「選擇要傳回的資訊 」。
關於顯示版權
向使用者顯示結果時,請務必加入下列版權聲明:
Powered by Google, ©YEAR Google
例如:
Powered by Google, ©2023 Google
在回應中指出路線
請使用 originIndex
和 destinationIndex
回應屬性,在回應中識別每個路線。這些屬性包含用於計算路線的輸入起點 和輸入目的地 路線點的以零為基點 索引。也就是說,請求中的第一個起點路線點和第一個目的地路線點位於陣列索引 0。
例如,回應中的路線包含:
"originIndex" : 1 ,
"destinationIndex" : 0
這些屬性表示路線是從輸入陣列中索引 1 的起點路標和索引 0 的目的地路標計算而得。
針對下一個路線:
"originIndex" : 4 ,
"destinationIndex" : 1
路線是從輸入陣列中索引 4 的起點路標,以及索引 1 的目的地路標計算而得。
關於回應
代表 API 回應的 JSON 物件 包含陣列,其中每個陣列元素都會定義從一個起點路線點到一個目的地路線點的路線。
除了 originIndex
和 destinationIndex
屬性之外,每個路線都包含該路線專屬的資訊,例如 distanceMeters
、duration
和 travelAdvisory
。
回應的格式如下:
[
// One array element for each combination of origin and destination waypoints.
{
// The route for source waypoint 0 and destination waypoint 0.
"originIndex" : 0 ,
"destinationIndex" : 0 ,
"status" : {
objec t (S tatus )
},
"condition" : e nu m (Rou te Ma tr ixEleme nt Co n di t io n ) ,
"distanceMeters" : i nte ger ,
"duration" : s tr i n g ,
"staticDuration" : s tr i n g ,
"travelAdvisory" : {
objec t (Rou te TravelAdvisory)
},
"fallbackInfo" : {
objec t (FallbackI nf o)
}
}
{
// The route for source waypoint 0 and destination waypoint 1.
"originIndex" : 0 ,
"destinationIndex" : 1 ,
"status" : {
objec t (S tatus )
},
"condition" : e nu m (Rou te Ma tr ixEleme nt Co n di t io n ) ,
"distanceMeters" : i nte ger ,
"duration" : s tr i n g ,
"staticDuration" : s tr i n g ,
"travelAdvisory" : {
objec t (Rou te TravelAdvisory)
},
"fallbackInfo" : {
objec t (FallbackI nf o)
}
}
…
]
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權 ,程式碼範例則為阿帕契 2.0 授權 。詳情請參閱《Google Developers 網站政策 》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-06-20 (世界標準時間)。
[null,null,["上次更新時間:2025-06-20 (世界標準時間)。"],[[["\u003cp\u003eThe Routes API's Route Matrix feature calculates routes between multiple origin and destination waypoints, providing travel distance and duration for each route combination.\u003c/p\u003e\n"],["\u003cp\u003eThe API response includes an array of route elements, each containing information like distance, duration, and advisory details for a specific origin-destination pair.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can use \u003ccode\u003eoriginIndex\u003c/code\u003e and \u003ccode\u003edestinationIndex\u003c/code\u003e properties to identify the origin and destination waypoints corresponding to each route element in the response.\u003c/p\u003e\n"],["\u003cp\u003eWhile the Route Matrix offers an overview of travel options, it doesn't provide detailed route information; use Compute Routes for turn-by-turn directions.\u003c/p\u003e\n"],["\u003cp\u003eWhen displaying results to users, you must include the copyright statement: \u003ccode\u003ePowered by Google, ©<YEAR> Google\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["# Review the route matrix response\n\n**European Economic Area (EEA) developers** If your billing address is in the European Economic Area, effective on 8 July 2025, the [Google Maps Platform EEA Terms of Service](https://cloud.google.com/terms/maps-platform/eea) will apply to your use of the Services. Functionality varies by region. [Learn more](/maps/comms/eea/faq).\n\nA route matrix request takes as input an array of origin and destination\nwaypoints. The Routes API then calculates the route from each origin\nwaypoint to each destination waypoint. For example, if you specify two origin\nwaypoints and two destination waypoints, the API then calculates four separate\nroutes.\n\nThe API response contains an array, where each array element corresponds to the\nroute from one origin waypoint to one destination waypoint. Therefore, if you\nspecify two origin waypoints and two destination waypoints, the response array\ncontains four elements, one for each computed route.\n\nUsing the response, you can provide your customers with different route options\nand then let the user select the appropriate route for their requirements.\n| **Note:** Route matrix does not return detailed route information, such as the legs, steps, and polylines of the route. To obtain detailed route information, pass a single origin and single destination to [Compute Routes](/maps/documentation/routes/compute_route_directions).\n\nAbout field masks\n-----------------\n\nWhen you call a method to compute a route, you must specify a field\nmask that defines which fields you want returned in the response. There is no\ndefault list of returned fields. If you omit this list, the methods return an\nerror.\n\nThe examples in this document show the entire response object without taking\nfield masks into consideration. In a production environment, your response would\nonly include the fields that you explicitly specify in the field mask.\n\nFor more information, see [Choose what information to return](/maps/documentation/routes/choose_fields).\n\nAbout displaying copyrights\n---------------------------\n\nYou must include the following copyright statement when displaying the results to your users:\n\n`Powered by Google, ©`\u003cvar translate=\"no\"\u003eYEAR\u003c/var\u003e` Google`\n\nFor example:\n\n`Powered by Google, ©2023 Google`\n\nIdentify the route in the response\n----------------------------------\n\nUse the `originIndex` and `destinationIndex` response properties to identify\neach route in the response. These properties contain the **zero-based** index of\nthe input **origin** and input **destination** waypoints used to calculate the\nroute. That means the first origin waypoint and the first destination waypoint\nin the request are at array index 0.\n\nFor example, a route in the response contains: \n\n```json\n\"originIndex\": 1,\n\"destinationIndex\": 0\n```\n\nThese properties indicate that the route was calculated from the origin\nwaypoint at index 1 in the input array, and from the destination waypoint at\nindex 0.\n\nFor the next route: \n\n```json\n\"originIndex\": 4,\n\"destinationIndex\": 1\n```\n\nThe route was calculated from the origin waypoint at index 4 in the input\narray, and from the destination waypoint at index 1 in the input array.\n\nAbout the response\n------------------\n\nThe\n[JSON object](/maps/documentation/routes/reference/rest/v2/TopLevel/computeRouteMatrix#response-body)\nrepresenting the API response contains an array, where each array element\ndefines the route from one origin waypoint to one destination waypoint.\n\nAlong with the `originIndex` and `destinationIndex` properties, each route\ncontains information specific to that route, such as `distanceMeters`,\n`duration`, and `travelAdvisory`.\n\nThe response has the form: \n\n```json\n[\n // One array element for each combination of origin and destination waypoints.\n {\n // The route for source waypoint 0 and destination waypoint 0.\n \"originIndex\": 0,\n \"destinationIndex\": 0,\n \"status\": {\n object (Status)\n },\n \"condition\": enum (RouteMatrixElementCondition),\n \"distanceMeters\": integer,\n \"duration\": string,\n \"staticDuration\": string,\n \"travelAdvisory\": {\n object (RouteTravelAdvisory)\n },\n \"fallbackInfo\": {\n object (FallbackInfo)\n }\n }\n {\n // The route for source waypoint 0 and destination waypoint 1.\n \"originIndex\": 0,\n \"destinationIndex\": 1,\n \"status\": {\n object (Status)\n },\n \"condition\": enum (RouteMatrixElementCondition),\n \"distanceMeters\": integer,\n \"duration\": string,\n \"staticDuration\": string,\n \"travelAdvisory\": {\n object (RouteTravelAdvisory)\n },\n \"fallbackInfo\": {\n object (FallbackInfo)\n }\n }\n ...\n]\n```"]]