ย้ายข้อมูลไปยังคลาสเมทริกซ์เส้นทาง
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คลาส RouteMatrix
จะแทนที่
บริการเมทริกซ์ระยะทาง, Maps JavaScript API (เดิม)
หน้านี้จะอธิบายความแตกต่างระหว่างบริการ Distance Matrix เดิมกับไลบรารี JavaScript ใหม่ และแสดงโค้ดบางส่วนเพื่อเปรียบเทียบ
Distance Matrix API (เดิม) เทียบกับคลาสเมทริกซ์เส้นทาง (เบต้า)
ตารางต่อไปนี้จะเปรียบเทียบพารามิเตอร์คำขอสำหรับ
Distance Matrix API แบบเดิมและ
คลาส RouteMatrix
การเปรียบเทียบโค้ด
ส่วนนี้จะเปรียบเทียบโค้ด 2 ส่วนที่คล้ายกันเพื่อแสดงความแตกต่างระหว่าง
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 ใหม่
// 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 Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-09-04 UTC
[null,null,["อัปเดตล่าสุด 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```"]]