Get a route Stay organized with collections Save and categorize content based on your preferences.
A route is a navigable path between a start location, or origin, and an end location, or destination. You can choose to get a route for different modes of transportation, such as walking, biking, or different types of vehicles. You can also request route details such as distance, estimated time to navigate the route, expected tolls, and step-by-step instructions to navigate the route.
Call thecomputeRoutes()
method to request a route between two locations. The following example shows defining a request and then callingcomputeRoutes()
to get a route.
// Import the Routes library.const{Route}=awaitgoogle.maps.importLibrary('routes');// Define a computeRoutes request.constrequest={origin:'Mountain View, CA',destination:'San Francisco, CA',};// Call the computeRoutes() method to get routes.const{routes}=awaitRoute.computeRoutes(request);
Choose fields to return
When you request a route, you must use a field mask to specify what information the response should return. You can specify the names of theRoute class properties in the field mask.
Using a field mask also ensures that you don't request unnecessary data, which in turn helps with response latency and avoids returning information your system doesn't need.
Specify the list of fields you need by setting theComputeRoutesRequest.fields
property, as shown in the following snippet:
TypeScript
// Define a routes request.constrequest={origin:'Mountain View, CA',destination:'San Francisco, CA',travelMode:'DRIVING',fields:['path'],// Request fields needed to draw polylines.};
JavaScript
// Define a routes request.constrequest={origin:'Mountain View, CA',destination:'San Francisco, CA',travelMode:'DRIVING',fields:['path'],// Request fields needed to draw polylines.};
Specify locations for a route
To calculate a route, you must specify at a minimum the locations of the route origin and route destination, and a field mask. You can also specify intermediate waypoints along a route, and use waypoints to do other things like adding stops or passthrough points along a route.
In theComputeRoutesRequest
, you can specify a location in any of the following ways:
- Place (preferred)
- Latitude/longitude coordinates
- Address string ("Chicago, IL" or "Darwin, NT, Australia")
- Plus Code
You can specify locations for all waypoints in a request the same way, or you can mix them. For example, you can use latitude/longitude coordinates for the origin waypoint and use a Place object for the destination waypoint.
For efficiency and accuracy, use Place objects instead of latitude/longitude coordinates or address strings. Place IDs are uniquely explicit and provide geocoding benefits for routing such as access points and traffic variables. They help avoid the following situations that can result from other ways of specifying a location:
- Using latitude/longitude coordinates can result in the location being snapped to the road nearest to those coordinates - which might not be an access point to the property, or even a road that quickly or safely leads to the destination.
- Address strings must first be geocoded by the Routes API to convert them to latitude/longitude coordinates before it can calculate a route. This conversion can affect performance.
Specify a location as a Place object (preferred)
To specify a location using a Place, create a newPlace
instance. The following snippet shows creating newPlace
instances fororigin
anddestination
, and then using them in aComputeRoutesRequest
:
TypeScript
// Use Place IDs in a directions request.constoriginPlaceInstance=newPlace({id:'ChIJiQHsW0m3j4ARm69rRkrUF3w',// Mountain View, CA});constdestinationPlaceInstance=newPlace({id:'ChIJIQBpAG2ahYAR_6128GcTUEo',// San Francisco, CA});constrequestWithPlaceIds={origin:originPlaceInstance,destination:destinationPlaceInstance,fields:['path'],// Request fields needed to draw polylines.};
JavaScript
// Use Place IDs in a directions request.constoriginPlaceInstance=newPlace({id:'ChIJiQHsW0m3j4ARm69rRkrUF3w',// Mountain View, CA});constdestinationPlaceInstance=newPlace({id:'ChIJIQBpAG2ahYAR_6128GcTUEo',// San Francisco, CA});constrequestWithPlaceIds={origin:originPlaceInstance,destination:destinationPlaceInstance,fields:['path'],// Request fields needed to draw polylines.};
Latitude/longitude coordinates
To specify a location as latitude/longitude coordinates, create either a newgoogle.maps.LatLngLiteral
,google.maps.LatLngAltitude
, orgoogle.maps.LatLngAltitudeLiteral
instance. The following snippet shows creating newgoogle.maps.LatLngLiteral
instances fororigin
anddestination
, and then using them in acomputeRoutesRequest
:
TypeScript
// Use lat/lng in a directions request.// Mountain View, CAconstoriginLatLng={lat:37.422000,lng:-122.084058};// San Francisco, CAconstdestinationLatLng={lat:37.774929,lng:-122.419415};// Define a computeRoutes request.constrequestWithLatLngs={origin:originLatLng,destination:destinationLatLng,fields:['path'],};
JavaScript
// Use lat/lng in a directions request.// Mountain View, CAconstoriginLatLng={lat:37.422000,lng:-122.084058};// San Francisco, CAconstdestinationLatLng={lat:37.774929,lng:-122.419415};// Define a computeRoutes request.constrequestWithLatLngs={origin:originLatLng,destination:destinationLatLng,fields:['path'],};
Address string
Address strings are literal addresses represented by a string (such as "1600 Amphitheatre Parkway, Mountain View, CA"). Geocoding is the process of converting an address string into latitudes and longitude coordinates (such as latitude 37.423021 and longitude -122.083739).
When you pass an address string as the location of a waypoint, the Routes library internally geocodes the string to convert it to latitude and longitude coordinates.
The following snippet shows creating aComputeRoutesRequest
with an address string fororigin
anddestination
:
TypeScript
// Use address strings in a directions request.constrequestWithAddressStrings={origin:'1600 Amphitheatre Parkway, Mountain View, CA',destination:'345 Spear Street, San Francisco, CA',fields:['path'],};
JavaScript
// Use address strings in a directions request.constrequestWithAddressStrings={origin:'1600 Amphitheatre Parkway, Mountain View, CA',destination:'345 Spear Street, San Francisco, CA',fields:['path'],};
Set the region for the address
If you pass an incomplete address string as the location of a waypoint, the API might use the wrong geocoded latitude/longitude coordinates. For example, you make a request specifying "Toledo" as the origin and "Madrid" as the destination for a driving route:
// Define a request with an incomplete address string.constrequest={origin:'Toledo',destination:'Madrid',};
In this example, "Toledo" is interpreted as a city in the state of Ohio in the United States, not in Spain. Therefore, the request returns an empty array, meaning no routes exist.
You can configure the API to return results biased to a particular region by including the regionCode parameter. This parameter specifies the region code as accTLD ("top-level domain") two-character value. Most ccTLD codes are identical to ISO 3166-1 codes, with some notable exceptions. For example, the United Kingdom's ccTLD is "uk" (.co.uk) while its ISO 3166-1 code is "gb" (technically for the entity of "The United Kingdom of Great Britain and Northern Ireland").
A directions request for "Toledo" to "Madrid" that includes the regionCode parameter returns appropriate results because "Toledo" is interpreted as a city in Spain:
constrequest={origin:'Toledo',destination:'Madrid',region:'es',// Specify the region code for Spain.};
Plus Code
Many people don't have a precise address, which can make it difficult for them to receive deliveries. Or, people with an address might prefer to accept deliveries at more specific locations, such as a back entrance or a loading dock.
Plus Codes are like street addresses for people or places that don't have an actual address. Instead of addresses with street names and numbers, Plus Codes are based on latitude/longitude coordinates, and are displayed as numbers and letters.
Google developedPlus Codes to give the benefit of addresses to everyone and everything. A Plus Code is an encoded location reference, derived from latitude/longitude coordinates, that represents an area: 1/8000th of a degree by 1/8000th of a degree (about 14m x 14m at the equator) or smaller. You can use Plus Codes as a replacement for street addresses in places where they don't exist or where buildings are not numbered or streets are not named.
Plus Codes must be formatted as a global code or a compound code:
- global code is composed of a 4 character area code and 6 character or longer local code. For example, for the address "1600 Amphitheatre Parkway, Mountain View, CA", the global code is "849V" and the local code is "CWC8+R9". You then use the entire 10 character Plus Code to specify the location value as "849VCWC8+R9".
- compound code is composed of a 6 character or longer local code combined with an explicit location. For example, the address "450 Serra Mall, Stanford, CA 94305, USA" has a local code of "CRHJ+C3". For a compound address, combine the local code with the city, state, ZIP code, and country portion of the address in the form "CRHJ+C3 Stanford, CA 94305, USA".
The following snippet shows calculating a route by specifying a waypoint for the route origin and destination using Plus Codes:
TypeScript
// Use Plus Codes in a directions request.constrequestWithPlusCodes={origin:'849VCWC8+R9',// Mountain View, CAdestination:'CRHJ+C3 Stanford, CA 94305, USA',// Stanford, CAfields:['path'],};
JavaScript
// Use Plus Codes in a directions request.constrequestWithPlusCodes={origin:'849VCWC8+R9',// Mountain View, CAdestination:'CRHJ+C3 Stanford, CA 94305, USA',// Stanford, CAfields:['path'],};
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.