The Routes Library, Maps JavaScript API includes the
Route
and
RouteMatrix
classes, which replace the legacy
DirectionsService
and
DistanceMatrixService
objects. Load the library in your web application with
google.maps.importLibrary("routes")
(see Use
client-side SDKs for why browser applications should use this library
instead of calling the https://routes.googleapis.com web
service directly).
Route class
The Route
class provides the
computeRoutes()
method, which returns the ideal route between two locations. Provide directions with
real-time traffic for transit, biking, driving, 2-wheel motorized vehicles, or walking
between multiple locations.
RouteMatrix class
The RouteMatrix class returns distances and travel times for a matrix of routes
between different origin and destination locations. Calculate travel times and distances for
multiple destinations for up to 625 route elements.
Choose a class for your use case
- Route planning with multiple stops: Use
Route.computeRoutes(). It supports intermediate waypoints, stop optimization, alternative routes, and toll estimates. To draw the route on a 2D map, callcreatePolylines()andcreateWaypointAdvancedMarkers(). To render a route on a 3D map, use theRoute3DElement(<gmp-route-3d>) web component, or callcreate3DPolylines(),createPopover(), andcreateWaypointMarkers(). - Dispatch and nearest-location matching: Use
RouteMatrix.computeRouteMatrix()to compare travel times and distances across several origins and destinations, up to 625 route elements. For example, assign the closest driver to a pickup or pick the nearest warehouse for a delivery.