Di chuyển sang lớp Ma trận tuyến đường
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.
Lớp RouteMatrix
sẽ thay thế Dịch vụ ma trận khoảng cách, API Maps JavaScript (Cũ).
Trang này giải thích sự khác biệt giữa dịch vụ Distance Matrix cũ và thư viện JavaScript mới, đồng thời cung cấp một số mã để so sánh.
Distance Matrix API (Cũ) so với lớp Route Matrix (Beta)
Bảng sau đây so sánh các tham số yêu cầu cho Distance Matrix API cũ và lớp RouteMatrix
.
So sánh mã
Phần này so sánh 2 đoạn mã tương tự để minh hoạ sự khác biệt giữa Distance Matrix API cũ và lớp RouteMatrix
mới. Các đoạn mã này cho biết mã cần thiết trên mỗi API tương ứng để đưa ra yêu cầu chỉ đường và xem kết quả.
Directions API (Cũ)
Đoạn mã sau đây đưa ra yêu cầu ma trận khoảng cách bằng cách sử dụng Distance Matrix API cũ.
// 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,
);
});
Lớp Ma trận tuyến đường (Beta)
Đoạn mã sau đây đưa ra yêu cầu ma trận khoảng cách bằng cách sử dụng lớp Route Matrix mới:
// 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,));
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-09-04 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-09-04 UTC."],[],[],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```"]]