查看路由矩阵响应
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
路线矩阵请求的输入是出发地和目的地航点的数组。然后,Routes API 会计算从每个起点航点到每个目的地航点的路线。例如,如果您指定两个起点航点和两个目的地航点,则该 API 会计算四条单独的路线。
API 响应包含一个数组,其中每个数组元素都对应于从一个起点航点到一个目的地航点的路线。因此,如果您指定两个出发地航点和两个目的地航点,则响应数组将包含四个元素,每个计算路线对应一个元素。
您可以使用响应为客户提供不同的路线选项,然后让用户根据自己的需求选择合适的路线。
注意 : 路线矩阵不会返回详细的路线信息,例如路线的路段、步骤和多段线。如需获取详细路线信息,请向计算路线 传递单个出发地和单个目的地。
关于字段掩码
调用方法来计算路线时,您必须指定一个字段掩码,用于定义您希望在响应中返回哪些字段。没有返回字段的默认列表。如果您省略此列表,这些方法会返回错误。
本文档中的示例显示了整个响应对象,而未考虑字段掩码。在生产环境中,您的响应将仅包含您在字段掩码中明确指定的字段。
如需了解详情,请参阅选择要返回的信息 。
关于显示版权信息
向用户显示结果时,您必须添加以下版权声明:
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)
}
}
…
]
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可 获得了许可,并且代码示例已根据 Apache 2.0 许可 获得了许可。有关详情,请参阅 Google 开发者网站政策 。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-20。
[null,null,["最后更新时间 (UTC):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```"]]