获取路由矩阵
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
使用 Routes API 通过调用 computeRouteMatrix 方法 (REST) 或流式 ComputeRouteMatrix 方法 (gRPC) 来计算多个出发地和目的地之间的路线距离和时长。
给定出发地和目的地列表,该方法会计算从每个出发地开始、到每个目的地结束的路线的距离和时长。
获取公交路线矩阵
您还可以计算公交路线矩阵。如需查看示例,请参阅获取公交路线矩阵。
请求限制
计算路线矩阵方法会对使用地址或地点 ID 的航点以及元素强制执行以下请求限制。元素是路线矩阵中每个出发地和目的地之间的路线,因此元素数量等于出发地数量乘以目的地数量。例如,如果您有 10 个起点和 10 个目的地,则有 100 个元素:
对于非 TRANSIT
路线,元素数量不得超过 625 个。
如果您指定了 TRANSIT
路由,元素数量不得超过 100 个。
如果您指定 TRAFFIC_AWARE_OPTIMAL
,元素数量不得超过 100 个。如需详细了解 TRAFFIC_AWARE_OPTIMAL
,请参阅指定是否要添加流量数据以及如何添加。
如果您使用地址或地点 ID 指定起点或终点,则最多可通过这种方式指定 50 个起点或终点。
响应错误
计算路线矩阵方法的一项功能是,可以针对整个响应或单个响应元素返回错误。例如,如果请求格式有误(例如,没有来源),整个响应都会包含错误。
不过,如果错误适用于响应中的部分元素(例如,无法为起点和目的地的某个组合计算路线),则只有受错误影响的元素会返回错误代码。
gRPC 流式传输结果
ComputeRouteMatrix gRPC 方法接受出发地和目的地列表,并返回一个包含每个出发地和目的地组合的路线信息的流。由于结果会以流的形式返回,因此您无需等到计算出所有可能的路线组合后才能开始处理结果。
数据流返回的元素不一定按任何顺序返回。因此,每个响应元素都包含一个 origin_index
和一个 destination_index
。对于请求指定的起点和终点,路线起点对于给定元素等同于 origins[origin_index]
,路线终点等同于 destinations[destination_index]
。这些数组从 0 开始编号。请务必存储起始列表和目标列表的顺序。
计算路线矩阵示例
在 HTTP 请求中使用 computeRouteMatrix 方法计算路线矩阵。
HTTP 示例
以下示例展示了 computeRouteMatrix
HTTP 请求。在此示例中,您需要:
指定一个包含两个起点和两个目的地航点的数组。该方法会计算从每个起点到每个目的地的路线,因此响应中包含四条路线。
在数组中,第一个元素的索引为 0,第二个元素的索引为 1,以此类推。
添加响应字段掩码,以指定要返回的 response(REST)或 ComputeRoutesResponse(gRPC)的哪些字段。在此示例中,请将请求配置为针对每个路线返回 originIndex
、destinationIndex
、duration
、distanceMeters
、status
和 condition
。如需了解详情,请参阅选择要返回的字段。
curl -X POST -d '{
"origins": [
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.420761,
"longitude": -122.081356
}
}
},
"routeModifiers": { "avoid_ferries": true}
},
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.403184,
"longitude": -122.097371
}
}
},
"routeModifiers": { "avoid_ferries": true}
}
],
"destinations": [
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.420999,
"longitude": -122.086894
}
}
}
},
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.383047,
"longitude": -122.044651
}
}
}
}
],
"travelMode": "DRIVE",
"routingPreference": "TRAFFIC_AWARE"
}' \
-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: YOUR_API_KEY' \
-H 'X-Goog-FieldMask: originIndex,destinationIndex,duration,distanceMeters,status,condition' \
'https://routes.googleapis.com/distanceMatrix/v2:computeRouteMatrix'
响应包含所有起点和目的地航点组合的四条可能路线。
使用 originIndex
和 destinationIndex
响应字段标识响应中的每个路线。例如,响应中的 originIndex
为 1 表示从请求中 origins
数组的索引 1 处的航点计算出的路线。
[
{
"originIndex": 0,
"destinationIndex": 0,
"status": {},
"distanceMeters": 822,
"duration": "160s",
"condition": "ROUTE_EXISTS"
},
{
"originIndex": 1,
"destinationIndex": 0,
"status": {},
"distanceMeters": 2919,
"duration": "361s",
"condition": "ROUTE_EXISTS"
},
{
"originIndex": 1,
"destinationIndex": 1,
"status": {},
"distanceMeters": 5598,
"duration": "402s",
"condition": "ROUTE_EXISTS"
},
{
"originIndex": 0,
"destinationIndex": 1,
"status": {},
"distanceMeters": 7259,
"duration": "712s",
"condition": "ROUTE_EXISTS"
}
]
gRPC 示例
例如 gRPC 请求,请参阅 gRPC 请求示例中的示例。该页面上的 Java 示例同时调用了计算路线和计算路线矩阵。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-20。
[null,null,["最后更新时间 (UTC):2025-06-20。"],[[["\u003cp\u003eGoogle Maps Routes API calculates distance and duration for routes with multiple origins and destinations using the \u003ccode\u003ecomputeRouteMatrix\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eRequest limits apply to the number of elements (routes between origin-destination pairs), varying based on route type and traffic settings.\u003c/p\u003e\n"],["\u003cp\u003eResponses can contain errors for the entire request or individual route elements, providing granular feedback on route calculation success.\u003c/p\u003e\n"],["\u003cp\u003eThe gRPC method offers stream results, allowing processing of route information as it becomes available, without waiting for all calculations to complete.\u003c/p\u003e\n"],["\u003cp\u003eExamples demonstrate how to use the API with HTTP requests and interpret the response, identifying routes using origin and destination indices.\u003c/p\u003e\n"]]],["The Routes API calculates route distances and durations for multiple origins and destinations using `computeRouteMatrix`. It supports REST and gRPC methods, with gRPC offering streaming results. Key actions include specifying origins and destinations, setting travel modes (like transit), and handling request limits (e.g., element counts for `TRANSIT` or `TRAFFIC_AWARE_OPTIMAL` routes). Errors can be response-wide or element-specific. The response provides `originIndex`, `destinationIndex`, `duration`, `distanceMeters`, and `status` for each route.\n"],null,["# Get a route matrix\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\nUse the Routes API to calculate the distance and duration of a route for\nmultiple origins and destinations by calling the\n[computeRouteMatrix](/maps/documentation/routes/reference/rest/v2/TopLevel/computeRouteMatrix) method\n(REST) or the streaming\n[ComputeRouteMatrix](/maps/documentation/routes/reference/rpc/google.maps.routing.v2#google.maps.routing.v2.Routes.ComputeRouteMatrix)\nmethod (gRPC).\n\nGiven a list of origins and destinations, the method\ncalculates the distance and duration of a route starting at each origin\nand ending at each destination.\n\n### Get a transit route matrix\n\nYou can also compute a route matrix on transit. For an\nexample, see [Get a route matrix on transit](/maps/documentation/routes/transit-rm).\n\n### Request limits\n\nThe Compute Route Matrix methods enforce the following request\nlimits for waypoints using address or placeIDs, and for elements. Elements are\nthe routes between each origin and destination in a route matrix, so the number\nof elements is the number of origins *times* the number of destinations. For\nexample, if you have 10 origins and 10 destinations, you have 100 elements:\n\n- **The number of elements** cannot exceed 625 for routes\n that are not `TRANSIT` routes.\n\n- **If you specify a `TRANSIT` route**, the number of elements cannot exceed\n 100.\n\n- **If you specify `TRAFFIC_AWARE_OPTIMAL`** , the number of elements cannot\n exceed 100. For more information about `TRAFFIC_AWARE_OPTIMAL`, see\n [Specify how and if to include traffic data](/maps/documentation/routes/config_trade_offs).\n\n- **If you specify origins or destinations using address or place ID**,\n you specify up to 50 total this way.\n\n### Response errors\n\nOne feature of the Compute Route Matrix methods is that errors can\nbe returned either for the entire response or for individual response elements.\nFor example, the entire response contains an error if the request is malformed\n(for example, it has zero origins).\n\nHowever, if an error applies to a subset of\nelements in the response (for example, a route cannot be computed for one\ncombination of origin and destination), then only the elements affected by the\nerror return an error code.\n\n### gRPC Stream results\n\nThe [ComputeRouteMatrix](/maps/documentation/routes/reference/rpc/google.maps.routing.v2#google.maps.routing.v2.Routes.ComputeRouteMatrix)\ngRPC method takes in a list of origins and destinations and returns a stream\ncontaining route information for each combination of origin and destination.\nBecause the results are returned as a stream, you don't have to wait until all\npossible route combinations are calculated before you can start to process the\nresults.\n| **Note:** Stream results are not supported by the REST API.\n\nThe elements returned by the stream are not guaranteed to be returned in any\norder. Therefore, each response element contains an `origin_index` and a\n`destination_index`. For the origins and destinations specified by the request,\nthe route origin is equivalent to `origins[origin_index]` for a given element\nand the route destination is equivalent to `destinations[destination_index]`.\nThese arrays are zero-indexed. It is important to store the origin and\ndestination list orders.\n\nCompute a Route Matrix Examples\n-------------------------------\n\nUse the [computeRouteMatrix](/maps/documentation/routes/reference/rest/v2/TopLevel/computeRouteMatrix)\nmethod in an HTTP request to compute a route matrix.\n\n### HTTP example\n\nThe following example shows a `computeRouteMatrix` HTTP request. In this example\nyou:\n\n- Specify an array of two origin and two destination waypoints. The method\n calculates a route from each origin to each destination so the response\n contains four routes.\n\n In the array, the first element is at an index of 0, the second is index 1,\n and so forth.\n- Include a response field mask to specify which fields of the\n [response](/maps/documentation/routes/reference/rest/v2/TopLevel/computeRouteMatrix#response-body)\n (REST) or\n [ComputeRoutesResponse](/maps/documentation/routes/reference/rpc/google.maps.routing.v2#computeroutesresponse)\n (gRPC) to return. In this example, configure the request to return\n `originIndex`, `destinationIndex`, `duration`, `distanceMeters`, `status`,\n and `condition` for each route. For more information, see\n [Choose fields to return](/maps/documentation/routes/choose_fields).\n\n```json\ncurl -X POST -d '{\n \"origins\": [\n {\n \"waypoint\": {\n \"location\": {\n \"latLng\": {\n \"latitude\": 37.420761,\n \"longitude\": -122.081356\n }\n }\n },\n \"routeModifiers\": { \"avoid_ferries\": true}\n },\n {\n \"waypoint\": {\n \"location\": {\n \"latLng\": {\n \"latitude\": 37.403184,\n \"longitude\": -122.097371\n }\n }\n },\n \"routeModifiers\": { \"avoid_ferries\": true}\n }\n ],\n \"destinations\": [\n {\n \"waypoint\": {\n \"location\": {\n \"latLng\": {\n \"latitude\": 37.420999,\n \"longitude\": -122.086894\n }\n }\n }\n },\n {\n \"waypoint\": {\n \"location\": {\n \"latLng\": {\n \"latitude\": 37.383047,\n \"longitude\": -122.044651\n }\n }\n }\n }\n ],\n \"travelMode\": \"DRIVE\",\n \"routingPreference\": \"TRAFFIC_AWARE\"\n}' \\\n-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: YOUR_API_KEY' \\\n-H 'X-Goog-FieldMask: originIndex,destinationIndex,duration,distanceMeters,status,condition' \\\n'https://routes.googleapis.com/distanceMatrix/v2:computeRouteMatrix'\n```\n\nThe response contains the four possible routes for the combination of all origin\nand destination waypoints.\n| **Note:** For detailed description of the response, see [Understand the compute\n| route matrix response](/maps/documentation/routes/understand-rm-response).\n\nIdentify each route in the response by using the `originIndex` and\n`destinationIndex` response fields. For example, an `originIndex` of 1 in the\nresponse corresponds to a route calculated from the waypoint at index 1 of the\n`origins` array in the request. \n\n```json\n[\n {\n \"originIndex\": 0,\n \"destinationIndex\": 0,\n \"status\": {},\n \"distanceMeters\": 822,\n \"duration\": \"160s\",\n \"condition\": \"ROUTE_EXISTS\"\n },\n {\n \"originIndex\": 1,\n \"destinationIndex\": 0,\n \"status\": {},\n \"distanceMeters\": 2919,\n \"duration\": \"361s\",\n \"condition\": \"ROUTE_EXISTS\"\n },\n {\n \"originIndex\": 1,\n \"destinationIndex\": 1,\n \"status\": {},\n \"distanceMeters\": 5598,\n \"duration\": \"402s\",\n \"condition\": \"ROUTE_EXISTS\"\n },\n {\n \"originIndex\": 0,\n \"destinationIndex\": 1,\n \"status\": {},\n \"distanceMeters\": 7259,\n \"duration\": \"712s\",\n \"condition\": \"ROUTE_EXISTS\"\n }\n]\n```\n\n### gRPC examples\n\nFor example [gRPC](https://www.grpc.io/) requests, see the examples on\n[Example gRPC request](/maps/documentation/routes/compute_route_directions#grpc-distance-example). The Java\nexample on that page calls both the Compute Routes and\nCompute Route Matrix."]]