Migrate to the Route Matrix class
Stay organized with collections
Save and categorize content based on your preferences.
European Economic Area (EEA) developers
The RouteMatrix
class replaces the
Distance Matrix Service, Maps JavaScript API (Legacy).
This page explains the differences between the legacy Distance Matrix service and the new
JavaScript library, and provides some code for comparison.
Distance Matrix API (Legacy) versus Route Matrix class (Beta)
The following table compares the request parameters for the legacy
Distance Matrix API and
the RouteMatrix
class.
Code comparison
This section compares two similar pieces of code to illustrate the differences between the
legacy Distance Matrix API and the new RouteMatrix
class. The code snippets show
the code required on each respective API to make a directions request, and view the results.
Directions API (Legacy)
The following code makes a distance matrix request using the legacy 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 class (Beta)
The following code makes a distance matrix request using the new Route Matrix class:
// 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,));
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-09-09 UTC.
[null,null,["Last updated 2025-09-09 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```"]]