计算路线矩阵概览
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Compute Route Matrix 是 Routes API 服务中的一个方法,用于接受 HTTPS 请求,并返回不同出发地和目的地位置之间路线矩阵的距离和行程时间。支持计算多个目的地之间的行程时间与距离,最多可包含 625 个路线元素。
需要查询行车路线?如果您对路线方向感兴趣,请参阅计算路线概览。
正在迁移?如果您要从 Distance Matrix API(旧版)进行迁移,请参阅从为何迁移到 Routes API 开始的迁移说明。
为何使用计算路线矩阵
借助计算路线矩阵,您可以确定最有效的路线来安排调度,例如:
如何最好地分配一组工人来提取一系列包裹
确定将包裹从哪个仓库配送到最终目的地最合适
您可以使用计算路线矩阵做些什么?
借助“计算路线矩阵”功能,您可以获取多个起点和终点之间可能的路线的矩阵。您可以为每个服务器端请求指定多个起点和目的地,其中起点数量乘以目的地数量不得超过 625。您可以在计算整个矩阵之前流式传输响应的元素,从而缩短响应延迟时间。
您可以请求以下类型的路线数据,以确定在多个始发地和目的地之间为乘客、运输或工人安排路线的最有效方式:
所选出行方式的距离(以公里或英里为单位)
在有交通的情况下的预计行程时间
计算路线矩阵的运作方式
ComputeRouteMatrix
方法接受包含请求详细信息的 JSON 请求正文的 HTTP POST 请求。必需提供至少一个起点、两个或更多目的地、travelMode 以及一个字段掩码,以指定要返回的字段。
示例
curl -X POST -d '{
"origins": [
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.420761,
"longitude": -122.081356
}
}
}
},
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.403184,
"longitude": -122.097371
}
}
}
}
],
"destinations": [
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.420999,
"longitude": -122.086894
}
}
}
},
{
"waypoint": {
"location": {
"latLng": {
"latitude": 37.383047,
"longitude": -122.044651
}
}
}
}
],
"travelMode": "DRIVE"
}' \
-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'
响应包含所有起点和目的地航点组合的可能路线。
资源
下表汇总了通过 Routes API Compute Routes Matrix 方法提供的资源以及它返回的数据。
如何使用计算路线矩阵
可用的客户端库
如需查看适用于计算路线矩阵的可用客户端库列表,请参阅客户端库。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-20。
[null,null,["最后更新时间 (UTC):2025-06-20。"],[],[],null,["# Compute Route Matrix Overview\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\nCompute Route Matrix is a method in the Routes API service that accepts\nan HTTPS request and returns distances and travel times for a matrix of routes\nbetween different origin and destination locations. Calculate travel times and\ndistances for multiple destinations for up to 625 route elements.\n\n**Need Driving Routes?** If you are interested in route directions, see\n[Compute Routes Overview](/maps/documentation/routes/compute-route-over).\n\n**Migrating?** If you are migrating from the Distance Matrix API (Legacy),\nsee migration instructions starting with\n[Why migrate to the Routes API](/maps/documentation/routes/migrate-routes-why).\n\nWhy use Compute Route Matrix\n----------------------------\n\nWith Compute Route Matrix, you can determine the most efficient\nroutes for dispatch scheduling, such as:\n\n- How best to assign a set of workers to pick up a series of packages\n\n- Determine the best warehouse from which to ship packages to their final\n destination\n\nWhat you can do with Compute Route Matrix?\n------------------------------------------\n\nWith Compute Route Matrix, you can get a matrix of possible routes\nbetween many start and end locations. You can specify multiple origins and\ndestinations per server-side request with the number of origins times the number\nof destinations up to 625. You can stream elements of the response before the\nentire matrix has been calculated, lowering response latency.\n\nYou can request these kinds of route data to determine the most efficient way to\nroute passengers, shipments, or workers between multiple origins and\ndestinations:\n\n- **Distance in kilometers or miles for a selected travel mode**\n\n- **Estimated travel time in traffic**\n\nHow Compute Route Matrix works\n------------------------------\n\nThe `ComputeRouteMatrix` method accepts an HTTP POST request with\na JSON request body that contains the request details. Required are at least one\norigin, two or more destinations, travelMode, and a field mask to specify which\nfields to return. \n\n#### Example\n\n```console\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 },\n {\n \"waypoint\": {\n \"location\": {\n \"latLng\": {\n \"latitude\": 37.403184,\n \"longitude\": -122.097371\n }\n }\n }\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}' \\\n-H 'Content-Type: application/json' -H 'X-Goog-Api-Key: \u003cvar translate=\"no\"\u003eYOUR_API_KEY\u003c/var\u003e' \\\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 possible routes for the combination of all origin\nand destination waypoints.\n\n### Resources\n\nThe following table summarizes the resources available through the\nRoutes API Compute Route Matrix method, along with\nthe data it returns.\n\n\u003cbr /\u003e\n\n| Data resources | Data returned | Return format |\n|-----------------------------------------------------------------------|----------------------------------------------------------------------------------|---------------|\n| [ComputeRouteMatrix](/maps/documentation/routes/compute_route_matrix) | Returns an array of routes from one origin waypoint to one destination waypoint. | JSON |\n\n\u003cbr /\u003e\n\nHow to use Compute Route Matrix\n-------------------------------\n\n|---|------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| 1 | **Get set up** | Start with [Set up your Google Cloud project](/maps/documentation/routes/cloud-setup) and complete the setup instructions that follow. |\n| 2 | **Understand how the Routes API bills** | For information, see [Usage and billing](/maps/documentation/routes/usage-and-billing). |\n| 3 | **Compute matrix of routes and review the response** | For more information, see [Get a route matrix](/maps/documentation/routes/compute_route_matrix) and [Review the route matrix response](/maps/documentation/routes/understand-rm-response). |\n\n### Available client libraries\n\nFor a list of the available client libraries for\nCompute Route Matrix, see\n[Client libraries](/maps/documentation/routes/client-libraries).\n\nWhat's next\n-----------\n\n- [Get a route matrix](/maps/documentation/routes/compute_route_matrix)\n- [Available route matrix options](/maps/documentation/routes/options-rm)\n- [Choose what information to return](/maps/documentation/routes/choose_fields-rm)\n- [Migrate from Distance Matrix API (Legacy)](/maps/documentation/routes/migrate-routes)\n- [Migrate from the Routes API preview to GA](/maps/documentation/routes/migrate-routes-preview)"]]