此产品或功能处于旧版状态。如需详细了解“旧版”阶段以及如何从旧版服务迁移到较新服务,请参阅
旧版产品和功能。
Distance Matrix API 概览
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
简介
距离矩阵 API(旧版)是一项服务,可接受包含指定运输方式的出发地和目的地的 HTTPS 请求。对于每对出发地和目的地,它都会返回行程距离和时长。
为何要使用 Distance Matrix API
您可以使用 Distance Matrix API 来帮助确定多个可能的出发地与目的地之间最有效的出行路线。例如,将哪些工人派往工作地点,或从哪些仓库发送包裹。
Distance Matrix API 的用途
借助 Distance Matrix API,您可以获取一系列出发地和目的地之间的行程距离和用时。您可以指定多种选项,包括交通方式(例如驾车、骑车、乘坐公交或步行)以及公交方式(例如公交车、地铁、火车、电车或轨道交通)。
Distance Matrix API 会根据起点和终点之间的推荐路线提供信息。您可以请求以下类型的距离数据:
- 所选出行方式的距离
- 距离(以公里或英里为单位)
- 交通状况下的预计行程时间

Distance Matrix API 的运作方式
距离矩阵 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 客户端是由社区提供支持的客户端库,并且根据 Apache 2.0 许可开放源代码。您可以从 GitHub 下载这些客户端库,还可以在其中找到安装说明和示例代码。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):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)."]]