Sản phẩm hoặc tính năng này đang ở trạng thái Cũ. Để biết thêm thông tin về giai đoạn Cũ và cách di chuyển từ các dịch vụ Cũ sang các dịch vụ mới hơn, hãy xem phần Các sản phẩm và tính năng cũ.
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nhà phát triển ở Khu vực kinh tế Châu Âu (EEA)
Giới thiệu
Distance Matrix API (cũ) là một dịch vụ chấp nhận yêu cầu HTTPS chứa điểm khởi hành và điểm đến cho một phương thức vận chuyển nhất định. Đối với mỗi tổ hợp điểm khởi hành và điểm đến, hàm này sẽ trả về quãng đường và thời gian di chuyển.
Lý do nên sử dụng Distance Matrix API
Bạn có thể sử dụng Distance Matrix API để xác định tuyến đường di chuyển hiệu quả nhất giữa nhiều điểm xuất phát và điểm đến có thể có. Ví dụ: nhân viên nào sẽ được cử đến công trường hoặc gói hàng sẽ được gửi từ nhà kho nào.
Những việc bạn có thể làm với Distance Matrix API
Với Distance Matrix API, bạn có thể cung cấp khoảng cách và thời gian di chuyển cho một ma trận gồm các điểm xuất phát và điểm đến. Bạn có thể chỉ định một số lựa chọn, bao gồm cả phương tiện giao thông (chẳng hạn như lái xe, đi xe đạp, phương tiện công cộng hoặc đi bộ), cũng như phương tiện công cộng (chẳng hạn như xe buýt, tàu điện ngầm, tàu hoả, xe điện hoặc tàu điện).
Distance Matrix API cung cấp thông tin dựa trên tuyến đường được đề xuất giữa điểm bắt đầu và điểm kết thúc. Bạn có thể yêu cầu những loại dữ liệu khoảng cách sau:
Khoảng cách của một phương tiện đi lại đã chọn
Khoảng cách tính bằng kilômét hoặc dặm
Thời gian di chuyển ước tính trong điều kiện giao thông
Cách hoạt động của Distance Matrix API
Distance Matrix API sử dụng một số lượng bất kỳ điểm khởi hành (điểm bắt đầu) và điểm đến, đồng thời trả về khoảng cách và thời gian di chuyển giữa mỗi điểm khởi hành và tất cả điểm đến được yêu cầu, bắt đầu từ điểm khởi hành đầu tiên trong yêu cầu và tiến hành từng bước đến điểm khởi hành tiếp theo.
Ví dụ: nếu yêu cầu của bạn chỉ định A và B là điểm xuất phát, còn C và D là điểm đến, thì yêu cầu đó sẽ trả về khoảng cách và thời gian di chuyển theo thứ tự sau:
A đến C
A đến D
B2C
B đến D
Ví dụ sau đây cho thấy một yêu cầu cho 2 điểm xuất phát: Washington, DC và Boston, với một điểm đến duy nhất là Thành phố New York, NY, ở định dạng JSON:
Sau khi có khoá API, bạn có thể bắt đầu kiểm thử Distance Matrix API ngay trên trình duyệt. Hãy xem Yêu cầu mẫu trong hướng dẫn Bắt đầu sử dụng để biết các ví dụ.
3
Soạn một yêu cầu phức tạp hơn
Hãy thử một yêu cầu cung cấp nhiều điểm xuất phát và điểm đến. Tìm hiểu về các tham số không bắt buộc để hạn chế tính toán, chẳng hạn như thời gian khởi hành đã chỉ định hoặc các hạn chế về tuyến đường. Xem Tham số không bắt buộc trong hướng dẫn Yêu cầu và phản hồi của Ma trận khoảng cách.
4
Tìm hiểu những điều cơ bản về phản hồi
Khám phá các phản hồi dữ liệu để chuẩn bị sử dụng dữ liệu ma trận khoảng cách cho ứng dụng của bạn. Hãy xem Phản hồi ma trận khoảng cách để biết thông tin chi tiết.
5
Kết hợp dữ liệu ma trận khoảng cách vào ứng dụng của riêng bạn!
Bạn có thể sử dụng dữ liệu này để tính toán khoảng cách và thời gian di chuyển.
Các thư viện ứng dụng hiện có
Gọi API này bằng ngôn ngữ bạn chọn thông qua một trong các thư viện ứng dụng sau:
Ứng dụng Java, ứng dụng Python, ứng dụng Go và ứng dụng Node.js cho Dịch vụ Google Maps là các thư viện ứng dụng được cộng đồng hỗ trợ, có nguồn mở theo Giấy phép Apache 2.0.
Bạn có thể tải các tệp này xuống từ GitHub, nơi bạn cũng có thể tìm thấy hướng dẫn cài đặt và mã mẫu.
Bước tiếp theo
Bắt đầu sử dụng Ma trận khoảng cách: Chuyển đến phần Bắt đầu.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-04 UTC."],[[["\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)."]]