迁移到 Route Matrix 类
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
RouteMatrix
类取代了 Maps JavaScript API(旧版)的距离矩阵服务。
本页介绍了旧版距离矩阵服务与新的 JavaScript 库之间的区别,并提供了一些代码以供比较。
Distance Matrix API(旧版)与 Route Matrix 类(Beta 版)
下表比较了旧版 Distance Matrix API 和 RouteMatrix
类的请求参数。
代码比较
本部分将比较两段类似的代码,以说明旧版 Distance Matrix API 与新版 RouteMatrix
类之间的区别。这些代码段展示了在每个相应 API 上发出路线请求并查看结果所需的代码。
Directions API(旧版)
以下代码使用旧版 Distance Matrix API 发出距离矩阵请求。
// Define the request.
const request = {
origins: [{lat: 55.93, lng: -3.118}, 'Greenwich, England'],
destinations: ['Stockholm, Sweden', {lat: 50.087, lng: 14.421}],
travelMode: 'DRIVING',
drivingOptions: {
departureTime: new Date(Date.now()),
trafficModel: 'optimistic'
}
};
// Make the request.
service.getDistanceMatrix(request).then((response) => {
// Display the response.
document.getElementById("response").textContent = JSON.stringify(
response,
null,
2,
);
});
Route Matrix 类(Beta 版)
以下代码使用新的 Route Matrix 类发出距离矩阵请求:
// Define the request.
const request = {
origins: [{lat: 55.93, lng: -3.118}, 'Greenwich, England'],
destinations: ['Stockholm, Sweden', {lat: 50.087, lng: 14.421}],
travelMode: 'DRIVING',
departureTime: new Date(),
trafficModel: 'optimistic'
};
// Make the request.
const response = await RouteMatrix.computeRouteMatrix(request);
// Display the response.
document.getElementById("response").setValue(JSON.stringify(response, null, 2,));
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-09-04。
[null,null,["最后更新时间 (UTC):2025-09-04。"],[],[],null,["**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\nThe `RouteMatrix` class replaces the\n[Distance Matrix Service, Maps JavaScript API (Legacy)](/maps/documentation/javascript/legacy/distancematrix).\nThis page explains the differences between the legacy Distance Matrix service and the new\nJavaScript library, and provides some code for comparison.\n\nDistance Matrix API (Legacy) versus Route Matrix class (Beta)\n\nThe following table compares the request parameters for the legacy\n[Distance Matrix API](/maps/documentation/javascript/reference/next/distance-matrix) and\nthe [`RouteMatrix`](/maps/documentation/javascript/reference/route-matrix) class.\n\n| [Distance Matrix Service (Legacy)](/maps/documentation/javascript/legacy/distancematrix) | [`RouteMatrix`](/maps/documentation/javascript/reference/route-matrix) (Beta) |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| **Required Parameters** ||\n| [`origins`](/maps/documentation/javascript/legacy/distancematrix) | [`origins`](/maps/documentation/javascript/reference/route-matrix#ComputeRouteMatrixRequest.origins) |\n| [`destinations`](/maps/documentation/javascript/legacy/distancematrix) | [`destinations`](/maps/documentation/javascript/reference/route-matrix#ComputeRouteMatrixRequest.destinations) |\n| **Optional Parameters** ||\n| [`travelMode`](/maps/documentation/javascript/legacy/distancematrix) | [`travelMode`](/maps/documentation/javascript/reference/route-matrix#ComputeRouteMatrixRequest.travelMode) |\n| [`transitOptions`](/maps/documentation/javascript/legacy/distancematrix) | [`transitPreference`](/maps/documentation/javascript/reference/route-matrix#ComputeRouteMatrixRequest.transitPreference) |\n| [`arrivalTime`](/maps/documentation/javascript/legacy/distancematrix) | [`arrivalTime`](/maps/documentation/javascript/reference/route-matrix#ComputeRouteMatrixRequest.arrivalTime) |\n| [`drivingOptions`](/maps/documentation/javascript/legacy/distancematrix#driving_options) | [`departureTime`](/maps/documentation/javascript/reference/route-matrix#ComputeRouteMatrixRequest.departureTime), [`trafficModel`](/maps/documentation/javascript/reference/route-matrix#ComputeRouteMatrixRequest.traffic_model) |\n| [`unitSystem`](/maps/documentation/javascript/legacy/distancematrix#distance_matrix_requests) | [`units`](/maps/documentation/javascript/reference/route-matrix#ComputeRouteMatrixRequest.units) |\n| [`avoidHighways`](/maps/documentation/javascript/legacy/distancematrix#driving_options), [`avoidTolls`](/maps/documentation/javascript/legacy/distancematrix#driving_options) | [`RouteModifiers`](/maps/documentation/javascript/reference/route-matrix#RouteModifiers) |\n\nCode comparison\n\nThis section compares two similar pieces of code to illustrate the differences between the\nlegacy Distance Matrix API and the new `RouteMatrix` class. The code snippets show\nthe code required on each respective API to make a directions request, and view the results.\n\nDirections API (Legacy)\n\nThe following code makes a distance matrix request using the legacy Distance Matrix API. \n\n```javascript\n// Define the request.\nconst request = {\n origins: [{lat: 55.93, lng: -3.118}, 'Greenwich, England'],\n destinations: ['Stockholm, Sweden', {lat: 50.087, lng: 14.421}],\n travelMode: 'DRIVING',\n drivingOptions: {\n departureTime: new Date(Date.now()),\n trafficModel: 'optimistic'\n }\n};\n\n// Make the request.\nservice.getDistanceMatrix(request).then((response) =\u003e {\n // Display the response.\n document.getElementById(\"response\").textContent = JSON.stringify(\n response,\n null,\n 2,\n );\n});\n \n```\n\nRoute Matrix class (Beta)\n\n\nThe following code makes a distance matrix request using the new Route Matrix class: \n\n```javascript\n// Define the request.\nconst request = {\n origins: [{lat: 55.93, lng: -3.118}, 'Greenwich, England'],\n destinations: ['Stockholm, Sweden', {lat: 50.087, lng: 14.421}],\n travelMode: 'DRIVING',\n departureTime: new Date(),\n trafficModel: 'optimistic'\n};\n\n// Make the request.\nconst response = await RouteMatrix.computeRouteMatrix(request);\n\n// Display the response.\ndocument.getElementById(\"response\").setValue(JSON.stringify(response, null, 2,));\n \n```"]]