Distance Matrix API 總覽
Distance Matrix API (舊版) 是一項服務,可接受含有特定交通方式起點和目的地的 HTTPS 要求。針對每個出發地和目的地組合,傳回行程距離和所需時間。
使用 Distance Matrix API 的好處
您可以使用 Distance Matrix API,協助判斷多個可能起點和目的地之間最有效率的路線。例如,要將哪些工人派往工作地點,或從哪些倉庫寄送包裹。
Distance Matrix API 的用途
您可以使用 Distance Matrix API,為出發地和目的地矩陣提供移動距離和時間。您可以指定多個選項,包括行車、騎單車、大眾運輸或步行等交通方式,以及公車、地鐵、火車、電車或鐵路等大眾運輸方式。
Distance Matrix API 會根據起點和終點之間的建議路線提供資訊。您可以要求下列類型的距離資料:
- 所選交通模式的距離
- 距離單位為公里或英里
- 預估交通時間

Distance Matrix API 的運作方式
Distance Matrix API 會使用任意數量的起點 (起點) 和目的地,並傳回各起點與所有要求目的地之間的距離和行程時間,從要求中的第 1 個起點開始,逐一傳回。
舉例來說,如果您的要求指定 A 和 B 為起點,C 和 D 為目的地,系統會依以下順序傳回距離和行車時間:
以下範例顯示兩個出發地 (華盛頓特區和波士頓) 的請求,以及單一目的地 (紐約市),格式為 JSON:
https://maps.googleapis.com/maps/api/distancematrix/json
?destinations=New%20York%20City%2C%20NY
&origins=Washington%2C%20DC%7CBoston
&units=imperial
&key=YOUR_API_KEY
資源
下表概略說明 Distance Matrix API 可用的資源,以及該 API 傳回的資料。
資料資源 |
傳回的資料 |
傳回格式 |
距離矩陣
以地點 ID、地址或經緯度座標的形式提供起點和終點。
|
- 各個出發地和目的地之間的距離和交通時間
- 如果您已指定行車模式和出發時間,則會顯示在交通中停留的時間
|
|
如何使用 Distance Matrix API
1 |
做好準備 |
請先設定 Google Cloud 專案,然後完成後續的設定說明。 |
2 |
嘗試使用距離矩陣要求 |
取得 API 金鑰後,您就可以直接在瀏覽器中測試 Distance Matrix API。如需範例,請參閱入門指南中的「要求範例」。 |
3 |
撰寫更複雜的要求 |
請嘗試使用提供多個出發地和目的地的請求。瞭解可選參數,以限制計算作業,例如指定的出發時間或路線限制。請參閱距離矩陣要求和回應指南中的「選用參數」
。 |
4 |
瞭解回應的基本概念 |
探索資料回應,為應用程式使用距離矩陣資料做好準備。詳情請參閱距離矩陣回應。 |
5 |
將距離矩陣資料納入您自己的應用程式! |
您可以使用這項資料來計算行車距離和時間。 |
可用的用戶端程式庫
透過下列任一用戶端程式庫,以您選擇的語言呼叫此 API:
Google 地圖服務適用的 Java、Python、Go 和 Node.js 用戶端都是社群支援的用戶端程式庫,並依據 Apache 2.0 授權的條款提供開放原始碼。您可以從 GitHub 下載這些用戶端或相關貢獻項目,並查看安裝操作說明和程式碼範例。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-05-13 (世界標準時間)。
[null,null,["上次更新時間:2025-05-13 (世界標準時間)。"],[[["The Distance Matrix API calculates travel distance and time between multiple origins and destinations, supporting various modes of transport like driving, biking, transit, or walking."],["This API is ideal for optimizing logistics, such as determining efficient delivery routes or assigning workers to job sites based on travel time and distance."],["The API returns data in JSON or XML format, providing distance and duration information for each origin-destination pair, including traffic estimations for driving mode."],["While it doesn't provide turn-by-turn directions, you can use the Directions API for detailed route guidance between a single origin and destination."],["You can access the Distance Matrix API using client libraries for Java, Python, Go, and Node.js, allowing integration with your applications for custom solutions."]]],["The Distance Matrix API calculates travel distance and duration between multiple origins and destinations for various transport modes like driving, biking, transit, or walking. It takes an HTTPS request specifying origins and destinations and returns data for each origin-destination pair, in a step-wise fashion. Users can customize requests with parameters like transport mode, units, and departure times, to be used for travel efficiency. The service can return distance, and estimated travel time, including traffic conditions. It supports JSON and XML return format.\n"]]