Migrate to the Route Matrix class Stay organized with collections Save and categorize content based on your preferences.
TheRouteMatrix
class replaces theDistance 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 legacyDistance Matrix API and theRouteMatrix
class.
Code comparison
This section compares two similar pieces of code to illustrate the differences between the legacy Distance Matrix API and the newRouteMatrix
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.constrequest={origins:[{lat:55.93,lng:-3.118},'Greenwich, England'],destinations:['Stockholm, Sweden',{lat:50.087,lng:14.421}],travelMode:'DRIVING',drivingOptions:{departureTime:newDate(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.constrequest={origins:[{lat:55.93,lng:-3.118},'Greenwich, England'],destinations:['Stockholm, Sweden',{lat:50.087,lng:14.421}],travelMode:'DRIVING',departureTime:newDate(),trafficModel:'optimistic'};// Make the request.constresponse=awaitRouteMatrix.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 theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-10-02 UTC.