這項產品或功能為舊版。如要進一步瞭解舊版階段和如何從舊版服務遷移至新版服務,請參閱「
舊版產品和功能」。
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 會使用任意數量的起點和目的地,並傳回每個起點與所有要求目的地之間的距離和行程時間,從要求中的第一個起點開始,逐步前往下一個起點。
舉例來說,如果要求指定 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 提供的資源,以及傳回的資料。
資料資源 |
傳回的資料 |
傳回格式 |
距離矩陣
以地點 ID、地址或經緯度座標的形式提供起點和目的地。
|
- 每個出發地和每個目的地之間的距離和交通時間
- 如果已指定駕駛模式和出發時間,系統會顯示塞車時的預計時間
|
|
如何使用 Distance Matrix API
1 |
做好準備 |
請先按照「設定 Google Cloud 專案」一文中的操作說明完成設定, |
2 |
試試距離矩陣要求 |
取得 API 金鑰後,您就可以直接在瀏覽器中開始測試 Distance Matrix API。如需範例,請參閱入門指南中的「要求範例」。 |
3 |
撰寫更複雜的要求 |
請嘗試提供多個起點和目的地的要求。瞭解可限制計算的可選參數,例如指定出發時間或路線限制。請參閱距離矩陣要求和回應指南中的「選用參數」。
|
4 |
瞭解回覆的基本概念 |
請探索資料回應,準備在應用程式中使用距離矩陣資料。詳情請參閱「距離矩陣回應」。 |
5 |
將距離矩陣資料整合到自己的應用程式! |
您可以使用這項資料計算旅行距離和時間。
|
可用的用戶端程式庫
透過下列任一用戶端程式庫,以您選擇的語言呼叫這項 API:
Google 地圖服務適用的 Java、Python、Go 和 Node.js 用戶端都是社群支援的用戶端程式庫,並依據阿帕契 2.0 授權的條款提供開放原始碼。您可以從 GitHub 下載這些用戶端,並查看安裝操作說明和程式碼範例。
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThe Distance Matrix API calculates travel distance and time between multiple origins and destinations, supporting various modes of transport like driving, biking, transit, or walking.\u003c/p\u003e\n"],["\u003cp\u003eThis API is ideal for optimizing logistics, such as determining efficient delivery routes or assigning workers to job sites based on travel time and distance.\u003c/p\u003e\n"],["\u003cp\u003eThe API returns data in JSON or XML format, providing distance and duration information for each origin-destination pair, including traffic estimations for driving mode.\u003c/p\u003e\n"],["\u003cp\u003eWhile it doesn't provide turn-by-turn directions, you can use the Directions API for detailed route guidance between a single origin and destination.\u003c/p\u003e\n"],["\u003cp\u003eYou can access the Distance Matrix API using client libraries for Java, Python, Go, and Node.js, allowing integration with your applications for custom solutions.\u003c/p\u003e\n"]]],["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"],null,["# Distance Matrix API 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\nIntroduction\n------------\n\nThe Distance Matrix API (Legacy) is a service that accepts an HTTPS request\ncontaining origins and destinations for a given mode of transport. For each\ncombination of origin and destination, it returns travel distance and duration. \n\nWhy use the Distance Matrix API\n-------------------------------\n\nYou can use the Distance Matrix API to help determine the most efficient\ntravel routes between multiple possible origins and destinations. For\nexample, which workers to send to job sites, or from which warehouses to send\npackages.\n| This API is now in [legacy\n| mode](https://developers.google.com/maps/legacy). Use [Compute Route Matrix](/maps/documentation/routes/compute-route-matrix-over) instead.\n\nWhat you can do with the Distance Matrix API\n--------------------------------------------\n\nWith the Distance Matrix API, you can provide travel distance and time for\na matrix of origins and destinations. You can specify several options, including\nmode of transportation, such as driving, biking, transit or walking, as well as\ntransit modes, such as bus, subway, train, tram, or rail.\n\nThe Distance Matrix API provides information based on the recommended route\nbetween start and end points. You can request these kinds of distance data:\n\n- Distance for a selected travel mode\n- Distance in kilometers or miles\n- Estimated travel time in traffic\n\nHow the Distance Matrix API works\n---------------------------------\n\nThe Distance Matrix API uses any number of origins (starting points) and destinations, and returns the distance and travel time between each\norigin and all requested destinations, starting with the first origin in\nthe request and proceeding step-wise to the next.\n\nFor example, if your request specifies A and B as origins, and C and D as destinations, it returns distances and travel time in this order:\n\n- A to C\n- A to D\n- B to C\n- B to D\n\nThe following example shows a request for two origins:\nWashington, DC and Boston, with a single destination, New York City, NY, in\nJSON format: \n\n```scdoc\nhttps://maps.googleapis.com/maps/api/distancematrix/json\n ?destinations=New%20York%20City%2C%20NY\n &origins=Washington%2C%20DC%7CBoston\n &units=imperial\n &key=YOUR_API_KEY\n```\n\n### Resources\n\nThe following table summarizes the resources available through the\nDistance Matrix API along with the data it returns.\n\n\u003cbr /\u003e\n\n| Data resources | Data returned | Return format |\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------|---------------|\n| [Distance matrix](/maps/documentation/distance-matrix/distance-matrix) Supply origins and destinations in the form of place IDs, addresses, or latitude/longitude coordinates. | - Distance and travel time between each origin and each destination - Duration in traffic if you've specified the driving mode and a departure time | - JSON - XML |\n\n\u003cbr /\u003e\n\nHow to use the Distance Matrix API\n----------------------------------\n\n|---|---------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| 1 | **Get set up** | Start with [Set up your Google Cloud project](/maps/documentation/distance-matrix/cloud-setup) and complete the setup instructions that follow. |\n| 2 | **Try a distance matrix request** | Once you have an API key, you can start testing out the Distance Matrix API directly from your browser. See [Sample requests](/maps/documentation/distance-matrix/start#sample-request) in the *Get started* guide for examples. |\n| 3 | **Compose a more complex request** | Try a request that supplies multiple origins and destinations. Learn about optional parameters to constrain calculations, such as a specified departure time, or route restrictions. See [Optional parameters](/maps/documentation/distance-matrix/distance-matrix#optional-parameters) in the *Distance Matrix request and response* guide. |\n| 4 | **Understand response basics** | Explore the data responses to prepare to use distance matrix data for your app. See [Distance matrix responses](/maps/documentation/distance-matrix/distance-matrix#distance-matrix-responses) for details. |\n| 5 | **Incorporate distance matrix data into your own app!** | You can use this data to calculate travel distance and time. |\n\n### Available client libraries\n\nCall this API in the language of\nyour choice through one of the following client libraries:\n\n- [Java\n Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-java)\n- [Python\n Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-python)\n- [Go Client\n for Google Maps Services](https://github.com/googlemaps/google-maps-services-go)\n- [Node.js\n Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-js)\n\nThe Java Client, Python Client, Go Client and Node.js Client for Google Maps\nServices are community-supported client libraries, open sourced under the\n[Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0).\nDownload them from GitHub, where you can also find installation instructions and sample code.\n\nWhat's next\n-----------\n\n- **Start using the Distance Matrix** : Go to [Get Started](/maps/documentation/distance-matrix/start).\n- **Get started with sample requests and responses** : Go to [Distance Matrix\n request and response](/maps/documentation/distance-matrix/distance-matrix).\n- **Follow best practices** : Go to [Web Service Best\n Practices](/maps/documentation/distance-matrix/web-service-best-practices)."]]