Routes Preferred API 目前仅适用于部分客户。如需了解详情,请
与销售人员联系。
Method: computeRouteMatrix
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
接受出发地和目的地的列表,并返回包含每种出发地和目的地组合的路线信息的数据流。
注意:此方法要求您在输入中指定响应字段掩码。您可以使用网址参数 $fields
或 fields
,或使用 HTTP/gRPC 标头 X-Goog-FieldMask
提供响应字段掩码(请参阅可用的网址参数和标头)。该值是以英文逗号分隔的字段路径列表。请参阅有关如何构建字段路径的详细文档。
例如,在此方法中:
- 所有可用字段的字段掩码(适用于人工检查):
X-Goog-FieldMask: *
- 路线时长、距离、元素状态、条件和元素索引的字段掩码(正式版设置示例):
X-Goog-FieldMask:
originIndex,destinationIndex,status,condition,distanceMeters,duration
请务必在字段掩码中添加 status
,否则所有消息都将显示为正常。Google 不建议使用通配符 (*
) 响应字段掩码,因为:
- 仅选择您需要的字段有助于我们的服务器节省计算周期,从而让我们能够以更短的延迟时间向您返回结果。
- 在生产作业中仅选择您需要的字段,以确保延迟性能稳定。我们未来可能会添加更多响应字段,而这些新字段可能需要额外的计算时间。如果您选择所有字段,或选择顶级的所有字段,则性能可能会下降,因为我们添加的任何新字段都会自动包含在响应中。
- 仅选择所需字段会使响应大小变小,从而提高网络吞吐量。
HTTP 请求
POST https://routespreferred.googleapis.com/v1alpha:computeRouteMatrix
网址采用 gRPC 转码语法。
请求正文
请求正文中包含结构如下的数据:
字段 |
origins[] |
object (RouteMatrixOrigin )
必需。来源数组,用于确定响应矩阵的行。出发地和目的地的基数有以下几种大小限制:
- 在任何情况下,元素(出发地 × 目的地)的数量都不得超过 625 个。
- 如果 routingPreference 设置为
TRAFFIC_AWARE_OPTIMAL ,元素数量(起点 × 终点)不得超过 100。
- 指定为
placeId 的路点(起点 + 终点)的数量不得超过 50 个。
|
destinations[] |
object (RouteMatrixDestination )
必需。目的地数组,用于确定响应矩阵的列。
|
travelMode |
enum (RouteTravelMode )
可选。指定交通方式。
|
routingPreference |
enum (RoutingPreference )
可选。指定如何计算路线。服务器会尝试使用所选的路由偏好设置来计算路由。如果路由偏好设置导致错误或延迟时间过长,则返回错误。只有在 travelMode 为 DRIVE 或 TWO_WHEELER 时才能指定此选项,否则请求会失败。
|
departureTime |
string (Timestamp format)
可选。出发时间。如果您未设置此值,则默认为您发出请求的时间。如果将此值设置为已经发生的时间,则请求将失败。 时间戳采用 RFC3339 世界协调时间(UTC,即“祖鲁时”)格式,精确到纳秒,最多九个小数位。示例:"2014-10-02T15:01:23Z" 和 "2014-10-02T15:01:23.045123456Z" 。
|
响应正文
如果成功,响应正文将包含 RouteMatrixElement
实例的流。
授权范围
需要以下 OAuth 范围:
https://www.googleapis.com/auth/maps-platform.routespreferred
如需了解详情,请参阅 OAuth 2.0 概览。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[[["\u003cp\u003eTakes a list of origins and destinations, returning route information for each combination.\u003c/p\u003e\n"],["\u003cp\u003eRequires specifying a response field mask for optimized performance and data selection.\u003c/p\u003e\n"],["\u003cp\u003eAccepts parameters for travel mode, routing preference, and departure time.\u003c/p\u003e\n"],["\u003cp\u003eReturns a stream of \u003ccode\u003eRouteMatrixElement\u003c/code\u003e instances containing route details.\u003c/p\u003e\n"],["\u003cp\u003eRequires the \u003ccode\u003ehttps://www.googleapis.com/auth/maps-platform.routespreferred\u003c/code\u003e OAuth scope for authorization.\u003c/p\u003e\n"]]],["This method computes route information for multiple origin-destination pairs. It requires a `POST` request to `computeRouteMatrix` with lists of `origins` and `destinations` in the request body. A field mask must be included to specify which data fields to return, with `status` being critical. Optional parameters include `travelMode`, `routingPreference`, and `departureTime`. The response is a stream of `RouteMatrixElement` instances, and the method requires a specific OAuth scope for authorization.\n"],null,["# Method: computeRouteMatrix\n\n- [HTTP request](#body.HTTP_TEMPLATE)\n- [Request body](#body.request_body)\n - [JSON representation](#body.request_body.SCHEMA_REPRESENTATION)\n- [Response body](#body.response_body)\n- [Authorization scopes](#body.aspect)\n\nTakes in a list of origins and destinations and returns a stream containing route information for each combination of origin and destination.\n\n**NOTE:** This method requires that you specify a response field mask in the input. You can provide the response field mask by using the URL parameter `$fields` or `fields`, or by using the HTTP/gRPC header `X-Goog-FieldMask` (see the [available URL parameters and headers](https://cloud.google.com/apis/docs/system-parameters). The value is a comma separated list of field paths. See this detailed documentation about [how to construct the field paths](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/field_mask.proto).\n\nFor example, in this method:\n\n- Field mask of all available fields (for manual inspection): `X-Goog-FieldMask: *`\n- Field mask of route durations, distances, element status, condition, and element indices (an example production setup): `X-Goog-FieldMask:\n originIndex,destinationIndex,status,condition,distanceMeters,duration`\n\nIt is critical that you include `status` in your field mask as otherwise all messages will appear to be OK. Google discourages the use of the wildcard (`*`) response field mask, because:\n\n- Selecting only the fields that you need helps our server save computation cycles, allowing us to return the result to you with a lower latency.\n- Selecting only the fields that you need in your production job ensures stable latency performance. We might add more response fields in the future, and those new fields might require extra computation time. If you select all fields, or if you select all fields at the top level, then you might experience performance degradation because any new field we add will be automatically included in the response.\n- Selecting only the fields that you need results in a smaller response size, and thus higher network throughput.\n\n### HTTP request\n\n`POST https://routespreferred.googleapis.com/v1alpha:computeRouteMatrix`\n\nThe URL uses [gRPC Transcoding](https://google.aip.dev/127) syntax.\n\n### Request body\n\nThe request body contains data with the following structure:\n\n| JSON representation |\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ``` { \"origins\": [ { object (/maps/documentation/routes_preferred/reference/rest/Shared.Types/ComputeRouteMatrixRequest#RouteMatrixOrigin) } ], \"destinations\": [ { object (/maps/documentation/routes_preferred/reference/rest/Shared.Types/ComputeRouteMatrixRequest#RouteMatrixDestination) } ], \"travelMode\": enum (/maps/documentation/routes_preferred/reference/rest/Shared.Types/RouteTravelMode), \"routingPreference\": enum (/maps/documentation/routes_preferred/reference/rest/Shared.Types/RoutingPreference), \"departureTime\": string } ``` |\n\n| Fields ||\n|-----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `origins[]` | `object (`[RouteMatrixOrigin](/maps/documentation/routes_preferred/reference/rest/Shared.Types/ComputeRouteMatrixRequest#RouteMatrixOrigin)`)` Required. Array of origins, which determines the rows of the response matrix. Several size restrictions apply to the cardinality of origins and destinations: - The number of elements (origins × destinations) must be no greater than 625 in any case. - The number of elements (origins × destinations) must be no greater than 100 if routingPreference is set to `TRAFFIC_AWARE_OPTIMAL`. - The number of waypoints (origins + destinations) specified as `placeId` must be no greater than 50. |\n| `destinations[]` | `object (`[RouteMatrixDestination](/maps/documentation/routes_preferred/reference/rest/Shared.Types/ComputeRouteMatrixRequest#RouteMatrixDestination)`)` Required. Array of destinations, which determines the columns of the response matrix. |\n| `travel``Mode` | `enum (`[RouteTravelMode](/maps/documentation/routes_preferred/reference/rest/Shared.Types/RouteTravelMode)`)` Optional. Specifies the mode of transportation. |\n| `routing``Preference` | `enum (`[RoutingPreference](/maps/documentation/routes_preferred/reference/rest/Shared.Types/RoutingPreference)`)` Optional. Specifies how to compute the route. The server attempts to use the selected routing preference to compute the route. If the routing preference results in an error or an extra long latency, an error is returned. You can specify this option only when the `travelMode` is `DRIVE` or `TWO_WHEELER`, otherwise the request fails. |\n| `departure``Time` | `string (`[Timestamp](https://protobuf.dev/reference/protobuf/google.protobuf/#timestamp)` format)` Optional. The departure time. If you don't set this value, this defaults to the time that you made the request. If you set this value to a time that has already occurred, the request fails. A timestamp in RFC3339 UTC \"Zulu\" format, with nanosecond resolution and up to nine fractional digits. Examples: `\"2014-10-02T15:01:23Z\"` and `\"2014-10-02T15:01:23.045123456Z\"`. |\n\n### Response body\n\nIf successful, the response body contains a stream of [RouteMatrixElement](/maps/documentation/routes_preferred/reference/rest/Shared.Types/RouteMatrixElement) instances.\n\n### Authorization scopes\n\nRequires the following OAuth scope:\n\n- `https://www.googleapis.com/auth/maps-platform.routespreferred`\n\nFor more information, see the [OAuth 2.0 Overview](https://developers.google.com/identity/protocols/OAuth2)."]]