Compute Route Matrix 簡介
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Compute Route Matrix 是 Routes API 服務中的一種方法,可接受 HTTPS 要求,並針對不同起點和目的地之間的路線矩陣,傳回距離和行車時間。計算多個目的地之間的行程時間和距離,最多可包含 625 個路線元素。
需要行車路線嗎?如果您對路線指示有興趣,請參閱Compute Routes 總覽。
遷移?如果您要從 Distance Matrix API (舊版) 遷移,請參閱「為何要遷移至 Routes API」一文,瞭解遷移操作說明。
使用 Compute Route Matrix 的好處
您可以使用 Compute Route Matrix,找出最有效率的調度排程路線,例如:
如何最佳分配一組工作人員來取件
判斷最適合的倉庫,將包裹運送至最終目的地
Compute Route Matrix 有哪些用途?
透過 Compute Route Matrix,您可以取得多個起點和終點之間可能的路線矩陣。您可以為每個伺服器端要求指定多個出發地和目的地,出發地和目的地數量乘積最多可達 625。您可以在計算整個矩陣之前,透過串流傳輸回應元素,藉此降低回應延遲時間。
您可以要求這類路線資料,以便判斷在多個起點和目的地之間,如何將乘客、貨物或工人轉送得最有效率:
所選交通方式的距離 (以公里或英里為單位)
預估交通時間
Compute Route Matrix 的運作方式
ComputeRouteMatrix
方法會接受 HTTP POST 要求,其中包含含有要求詳細資料的 JSON 要求主體。至少需要一個起點、兩個以上的目的地、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 Route Matrix 方法取得的資源,以及該方法傳回的資料。
如何使用 Compute Route Matrix
可用的用戶端程式庫
如需 Compute Route Matrix 可用用戶端程式庫的清單,請參閱「用戶端程式庫」。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-06-20 (世界標準時間)。
[null,null,["上次更新時間: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)"]]