DirectionsService class
google.maps.DirectionsService
class
A service for computing directions between two or more places.
Access by calling const {DirectionsService} = await google.maps.importLibrary("routes").
See Libraries in the Maps JavaScript API.
Constructor | |
|---|---|
DirectionsService |
DirectionsService()Parameters: None
Creates a new instance of a DirectionsService that sends directions queries to Google servers. |
Methods | |
|---|---|
route |
route(request[, callback])Parameters:
Return Value:
Promise<DirectionsResult>Issue a directions search request. |
DirectionsRequest interface
google.maps.DirectionsRequest
interface
A directions query to be sent to the DirectionsService.
Properties | |
|---|---|
destination |
Type:
string|LatLng|Place|LatLngLiteralLocation of destination. This can be specified as either a string to be geocoded, or a LatLng, or a Place. Required. |
origin |
Type:
string|LatLng|Place|LatLngLiteralLocation of origin. This can be specified as either a string to be geocoded, or a LatLng, or a Place. Required. |
travelMode |
Type:
TravelModeType of routing requested. Required. |
avoidFerries optional |
Type:
boolean optionalIf true, instructs the Directions service to avoid ferries where possible. Optional. |
avoidHighways optional |
Type:
boolean optionalIf true, instructs the Directions service to avoid highways where possible. Optional. |
avoidTolls optional |
Type:
boolean optionalIf true, instructs the Directions service to avoid toll roads where possible. Optional. |
drivingOptions optional |
Type:
DrivingOptions optionalSettings that apply only to requests where travelMode is DRIVING. This object will have no effect for other travel modes. |
language optional |
Type:
string optionalA language identifier for the language in which results should be returned, when possible. See the list of supported languages. |
optimizeWaypoints optional |
Type:
boolean optionalIf set to true, the DirectionsService will attempt to re-order the supplied intermediate waypoints to minimize overall cost of the route. If waypoints are optimized, inspect DirectionsRoute.waypoint_order in the response to determine the new ordering. |
provideRouteAlternatives optional |
Type:
boolean optionalWhether or not route alternatives should be provided. Optional. |
region optional |
Type:
string optionalRegion code used as a bias for geocoding requests. The region code accepts a ccTLD ("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"). |
transitOptions optional |
Type:
TransitOptions optionalSettings that apply only to requests where travelMode is TRANSIT. This object will have no effect for other travel modes. |
unitSystem optional |
Type:
UnitSystem optionalDefault: The unit system used in the country of origin.
Preferred unit system to use when displaying distance. |
waypoints optional |
Type:
Array<DirectionsWaypoint> optionalArray of intermediate waypoints. Directions are calculated from the origin to the destination by way of each waypoint in this array. See the developer's guide for the maximum number of waypoints allowed. Waypoints are not supported for transit directions. Optional. |
DirectionsStatus constants
google.maps.DirectionsStatus
constants
The status returned by the DirectionsService on the completion of a call to route(). Specify these by value, or by using the constant's name. For example, 'OK' or google.maps.DirectionsStatus.OK.
Access by calling const {DirectionsStatus} = await google.maps.importLibrary("routes").
See Libraries in the Maps JavaScript API.
Constants | |
|---|---|
INVALID_REQUEST |
The DirectionsRequest provided was invalid. |
MAX_WAYPOINTS_EXCEEDED |
Too many DirectionsWaypoints were provided in the DirectionsRequest. See the developer's guide for the maximum number of waypoints allowed. |
NOT_FOUND |
At least one of the origin, destination, or waypoints could not be geocoded. |
OK |
The response contains a valid DirectionsResult. |
OVER_QUERY_LIMIT |
The webpage has gone over the requests limit in too short a period of time. |
REQUEST_DENIED |
The webpage is not allowed to use the directions service. |
UNKNOWN_ERROR |
A directions request could not be processed due to a server error. The request may succeed if you try again. |
ZERO_RESULTS |
No route could be found between the origin and destination. |
DirectionsResult interface
google.maps.DirectionsResult
interface
The directions response retrieved from the directions server. You can render these using a DirectionsRenderer or parse this object and render it yourself. You must display the warnings and copyrights as noted in the Google Maps Platform Terms of Service. Note that though this result is "JSON-like," it is not strictly JSON, as it indirectly includes LatLng objects.
Properties | |
|---|---|
request |
Type:
DirectionsRequestThe DirectionsRequest that yielded this result. |
routes |
Type:
Array<DirectionsRoute>An array of DirectionsRoutes, each of which contains information about the legs and steps of which it is composed. There will only be one route unless the DirectionsRequest was made with provideRouteAlternatives set to true. |
available_travel_modes optional |
Type:
Array<TravelMode> optionalContains an array of available travel modes. This field is returned when a request specifies a travel mode and gets no results. The array contains the available travel modes in the countries of the given set of waypoints. This field is not returned if one or more of the waypoints are 'via waypoints'. |
geocoded_waypoints optional |
Type:
Array<DirectionsGeocodedWaypoint> optionalAn array of DirectionsGeocodedWaypoints, each of which contains information about the geocoding of origin, destination and waypoints. |
DirectionsRenderer class
google.maps.DirectionsRenderer
class
Renders directions obtained from the DirectionsService.
This class extends
MVCObject.
Access by calling const {DirectionsRenderer} = await google.maps.importLibrary("routes").
See Libraries in the Maps JavaScript API.
Constructor | |
|---|---|
DirectionsRenderer |
DirectionsRenderer([opts])Parameters:
Creates the renderer with the given options. Directions can be rendered on a map (as visual overlays) or additionally on a <div> panel (as textual instructions). |
Methods | |
|---|---|
getDirections |
getDirections()Parameters: None
Return Value:
DirectionsResultReturns the renderer's current set of directions. |
getMap |
getMap()Parameters: None
Return Value:
MapReturns the map on which the DirectionsResult is rendered. |
getPanel |
getPanel()Parameters: None
Return Value:
HTMLElement|nullReturns the panel <div> in which the DirectionsResult is rendered. |
getRouteIndex |
getRouteIndex()Parameters: None
Return Value:
numberReturns the current (zero-based) route index in use by this DirectionsRenderer object. |
setDirections |
setDirections(directions)Parameters:
Return Value: None
Set the renderer to use the result from the DirectionsService. Setting a valid set of directions in this manner will display the directions on the renderer's designated map and panel. |
setMap |
setMap(map)Parameters:
Return Value: None
This method specifies the map on which directions will be rendered. Pass null to remove the directions from the map. |
setOptions |
setOptions(options)Parameters:
Return Value: None
Change the options settings of this DirectionsRenderer after initialization. |
setPanel |
setPanel(panel)Parameters:
Return Value: None
This method renders the directions in a <div>. Pass null to remove the content from the panel. |
setRouteIndex |
setRouteIndex(routeIndex)Parameters:
Return Value: None
Set the (zero-based) index of the route in the DirectionsResult object to render. By default, the first route in the array will be rendered. |
Inherited:
addListener,
bindTo,
get,
notify,
set,
setValues,
unbind,
unbindAll
| |
Events | |
|---|---|
directions_changed |
function()Arguments: None
This event is fired when the rendered directions change, either when a new DirectionsResult is set or when the user finishes dragging a change to the directions path. |
DirectionsRendererOptions interface
google.maps.DirectionsRendererOptions
interface
This object defines the properties that can be set on a DirectionsRenderer object.
Properties | |
|---|---|
directions optional |
Type:
DirectionsResult optionalThe directions to display on the map and/or in a <div> panel, retrieved as a DirectionsResult object from DirectionsService. |
draggable optional |
Type:
boolean optionalIf true, allows the user to drag and modify the paths of routes rendered by this DirectionsRenderer. |
hideRouteList optional |
Type:
boolean optionalDefault:
falseThis property indicates whether the renderer should provide a user-selectable list of routes shown in the directions panel. |
infoWindow optional |
Type:
InfoWindow optionalThe InfoWindow in which to render text information when a marker is clicked. Existing info window content will be overwritten and its position moved. If no info window is specified, the DirectionsRenderer will create and use its own info window. This property will be ignored if suppressInfoWindows is set to true. |
map optional |
Type:
Map optionalMap on which to display the directions. |
markerOptions optional |
Type:
MarkerOptions optionalOptions for the markers. All markers rendered by the DirectionsRenderer will use these options. |
panel optional |
Type:
HTMLElement optionalThe <div> in which to display the directions steps. |
polylineOptions optional |
Type:
PolylineOptions optionalOptions for the polylines. All polylines rendered by the DirectionsRenderer will use these options. |
preserveViewport optional |
Type:
boolean optionalDefault:
falseIf this option is set to true or the map's center and zoom were never set, the input map is centered and zoomed to the bounding box of this set of directions. |
routeIndex optional |
Type:
number optionalThe index of the route within the DirectionsResult object. The default value is 0. |
suppressBicyclingLayer optional |
Type:
boolean optionalSuppress the rendering of the BicyclingLayer when bicycling directions are requested. |
suppressInfoWindows optional |
Type:
boolean optionalSuppress the rendering of info windows. |
suppressMarkers optional |
Type:
boolean optionalSuppress the rendering of markers. |
suppressPolylines optional |
Type:
boolean optionalSuppress the rendering of polylines. |
DirectionsWaypoint interface
google.maps.DirectionsWaypoint
interface
A DirectionsWaypoint represents a location between origin and destination through which the trip should be routed.
Properties | |
|---|---|
location optional |
Type:
string|LatLng|LatLngLiteral|Place optionalWaypoint location. Can be an address string, a LatLng, or a Place. Optional. |
stopover optional |
Type:
boolean optionalDefault:
trueIf true, indicates that this waypoint is a stop between the origin and destination. This has the effect of splitting the route into two legs. If false, indicates that the route should be biased to go through this waypoint, but not split into two legs. This is useful if you want to create a route in response to the user dragging waypoints on a map. |
DirectionsGeocodedWaypoint interface
google.maps.DirectionsGeocodedWaypoint
interface
A single geocoded waypoint.
Properties | |
|---|---|
partial_match optional |
Type:
boolean optionalWhether the geocoder did not return an exact match for the original waypoint, though it was able to match part of the requested address. |
place_id optional |
Type:
string optionalThe place ID associated with the waypoint. Place IDs uniquely identify a place in the Google Places database and on Google Maps. Learn more about Place IDs in the Places API developer guide. |
types optional |
Type:
Array<string> optionalAn array of strings denoting the type of the returned geocoded element. For a list of possible strings, refer to the Address Component Types section of the Developer's Guide. |
DirectionsRoute interface
google.maps.DirectionsRoute
interface
A single route containing a set of legs in a DirectionsResult. Note that though this object is "JSON-like," it is not strictly JSON, as it directly and indirectly includes LatLng objects.
Properties | |
|---|---|
bounds |
Type:
LatLngBoundsThe bounds for this route. |
copyrights |
Type:
stringCopyrights text to be displayed for this route. |
legs |
Type:
Array<DirectionsLeg>An array of DirectionsLegs, each of which contains information about the steps of which it is composed. There will be one leg for each stopover waypoint or destination specified. So a route with no stopover waypoints will contain one DirectionsLeg and a route with one stopover waypoint will contain two. |
overview_path |
An array of LatLngs representing the entire course of this route. The path is simplified in order to make it suitable in contexts where a small number of vertices is required (such as Static Maps API URLs). |
overview_polyline |
Type:
stringAn encoded polyline representation of the route in overview_path. This polyline is an approximate (smoothed) path of the resulting directions. |
summary |
Type:
stringContains a short textual description for the route, suitable for naming and disambiguating the route from alternatives. |
warnings |
Type:
Array<string>Warnings to be displayed when showing these directions. |
waypoint_order |
Type:
Array<number>If optimizeWaypoints was set to true, this field will contain the re-ordered permutation of the input waypoints. For example, if the input was:Origin: Los Angeles Waypoints: Dallas, Bangor, Phoenix Destination: New York and the optimized output was ordered as follows: Origin: Los Angeles Waypoints: Phoenix, Dallas, Bangor Destination: New York then this field will be an Array containing the values [2, 0, 1]. Note that the numbering of waypoints is zero-based.If any of the input waypoints has stopover set to false, this field will be empty, since route optimization is not available for such queries. |
fare optional |
Type:
TransitFare optionalThe total fare for the whole transit trip. Only applicable to transit requests. |
DirectionsLeg interface
google.maps.DirectionsLeg
interface
A single leg consisting of a set of steps in a DirectionsResult. Some fields in the leg may not be returned for all requests. Note that though this result is "JSON-like," it is not strictly JSON, as it directly and indirectly includes LatLng objects.
Properties | |
|---|---|
end_address |
Type:
stringThe address of the destination of this leg. This content is meant to be read as-is. Do not programmatically parse the formatted address. |
end_location |
Type:
LatLngThe DirectionsService calculates directions between locations by using the nearest transportation option (usually a road) at the start and end locations. end_location indicates the actual geocoded destination, which may be different than the end_location of the last step if, for example, the road is not near the destination of this leg. |
start_address |
Type:
stringThe address of the origin of this leg. This content is meant to be read as-is. Do not programmatically parse the formatted address. |
start_location |
Type:
LatLngThe DirectionsService calculates directions between locations by using the nearest transportation option (usually a road) at the start and end locations. start_location indicates the actual geocoded origin, which may be different than the start_location of the first step if, for example, the road is not near the origin of this leg. |
steps |
Type:
Array<DirectionsStep>An array of DirectionsSteps, each of which contains information about the individual steps in this leg. |
|
Type:
Array<*>Information about traffic speed along the leg. |
via_waypoints |
An array of non-stopover waypoints along this leg, which were specified in the original request. Deprecated in alternative routes. Version 3.27 will be the last version of the API that adds extra When using the Directions Service to implement draggable directions, it is recommended to disable dragging of alternative routes. Only the main route should be draggable. Users can drag the main route until it matches an alternative route. |
arrival_time optional |
Type: |