Class DirectionFinder

  • DirectionFinder allows you to get directions between locations.

  • You can specify an origin, destination, and waypoints using addresses or coordinates.

  • The class offers methods to set travel mode, language, and specify restrictions.

  • The getDirections() method returns a JSON object containing the route details.

DirectionFinder

Allows for the retrieval of directions between locations.
The example below shows how you can use this class to get the directions from Times Square to Central Park, stopping first at Lincoln Center, plot the locations and path on a map, and send the map in an email.

// Get the directions.
constdirections=Maps.newDirectionFinder()
.setOrigin('Times Square, New York, NY')
.addWaypoint('Lincoln Center, New York, NY')
.setDestination('Central Park, New York, NY')
.setMode(Maps.DirectionFinder.Mode.DRIVING)
.getDirections();
constroute=directions.routes[0];
// Set up marker styles.
letmarkerLetterCode='A'.charCodeAt();
// Add markers to the map.
constmap=Maps.newStaticMap();
for(leti=0;i < route.legs.length;i++){
constleg=route.legs[i];
if(i===0){
// Add a marker for the start location of the first leg only.
map.setMarkerStyle(
Maps.StaticMap.MarkerSize.MID,
Maps.StaticMap.Color.GREEN,
String.fromCharCode(markerLetterCode),
);
map.addMarker(leg.start_location.lat,leg.start_location.lng);
markerLetterCode++;
}
map.setMarkerStyle(
Maps.StaticMap.MarkerSize.MID,
Maps.StaticMap.Color.GREEN,
String.fromCharCode(markerLetterCode),
);
map.addMarker(leg.end_location.lat,leg.end_location.lng);
markerLetterCode++;
}
// Add a path for the entire route.
map.addPath(route.overview_polyline.points);
// Send the map in an email.
consttoAddress=Session.getActiveUser().getEmail();
MailApp.sendEmail(
toAddress,
'Directions',
`Please open: ${map.getMapUrl()}&key=YOUR_API_KEY`,
{
htmlBody:'See below.<br/><img src="cid:mapImage">',
inlineImages:{
mapImage:Utilities.newBlob(map.getMapImage(),'image/png'),
},
},
);

See also

Methods

MethodReturn typeBrief description
addWaypoint(latitude, longitude) DirectionFinder Adds a waypoint that the route must pass through, using a point (lat/lng).
addWaypoint(address) DirectionFinder Adds a waypoint that the route must pass through, using an address.
clearWaypoints() DirectionFinder Clears the current set of waypoints.
getDirections() ObjectGets the directions using the origin, destination, and other options that were set.
setAlternatives(useAlternatives) DirectionFinder Sets whether or not alternative routes should be returned, instead of just the highest ranked route (defaults to false).
setArrive(time) DirectionFinder Sets the desired time of arrival (when applicable).
setAvoid(avoid) DirectionFinder Sets whether to avoid certain types of restrictions.
setDepart(time) DirectionFinder Sets the desired time of departure (when applicable).
setDestination(latitude, longitude) DirectionFinder Sets the ending location for which to calculate directions to, using a point (lat/lng).
setDestination(address) DirectionFinder Sets the ending location for which to calculate directions to, using an address.
setLanguage(language) DirectionFinder Sets the language to be used for the directions.
setMode(mode) DirectionFinder Sets the mode of travel (defaults to driving).
setOptimizeWaypoints(optimizeOrder) DirectionFinder Sets whether or not to optimize the provided route by rearranging the waypoints in a more efficient order (defaults to false).
setOrigin(latitude, longitude) DirectionFinder Sets the starting location from which to calculate directions, using a point (lat/lng).
setOrigin(address) DirectionFinder Sets the starting location from which to calculate directions, using an address.
setRegion(region) DirectionFinder Sets a region to use when interpreting location names.

Detailed documentation

addWaypoint(latitude, longitude)

Adds a waypoint that the route must pass through, using a point (lat/lng).

// Creates a DirectionFinder with a wapoint at Lincoln Center.
constdirectionFinder=Maps.newDirectionFinder().addWaypoint(
40.772628,
-73.984243,
);

Parameters

NameTypeDescription
latitudeNumberLatitude of the waypoint.
longitudeNumberLongitude of the waypoint.

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.


addWaypoint(address)

Adds a waypoint that the route must pass through, using an address.

// Creates a DirectionFinder with a wapoint at Lincoln Center.
constdirectionFinder=Maps.newDirectionFinder().addWaypoint(
'Lincoln Center, New York, NY',
);

Parameters

NameTypeDescription
addressStringAn address.

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.


clearWaypoints()

Clears the current set of waypoints.

constdirectionFinder=Maps.newDirectionFinder();
// ...
// Do something interesting here ...
// ...
// Remove all waypoints added with addWaypoint().
directionFinder.clearWaypoints();

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.


getDirections()

Gets the directions using the origin, destination, and other options that were set.

// Logs how long it takes to walk from Times Square to Central Park.
constdirections=Maps.newDirectionFinder()
.setOrigin('Times Square, New York, NY')
.setDestination('Central Park, New York, NY')
.setMode(Maps.DirectionFinder.Mode.WALKING)
.getDirections();
Logger.log(directions.routes[0].legs[0].duration.text);

Return

Object — A JSON object containing the set of routes for the directions, as described here.

See also


setAlternatives(useAlternatives)

Sets whether or not alternative routes should be returned, instead of just the highest ranked route (defaults to false). If true, the resulting object's routes array may contain multiple entries.

// Creates a DirectionFinder with alternative routes enabled.
constdirectionFinder=Maps.newDirectionFinder().setAlternatives(true);

Parameters

NameTypeDescription
useAlternativesBooleantrue to return alternative routes, false otherwise.

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.


setArrive(time)

Sets the desired time of arrival (when applicable).

// Creates a DirectionFinder with an arrival time of 2 hours from now.
constnow=newDate();
constarrive=newDate(now.getTime()+2*60*60*1000);
constdirectionFinder=Maps.newDirectionFinder().setArrive(arrive);

Parameters

NameTypeDescription
timeDateThe time of arrival.

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.

See also


setAvoid(avoid)

Sets whether to avoid certain types of restrictions.

// Creates a DirectionFinder that avoid highways.
constdirectionFinder=Maps.newDirectionFinder().setAvoid(
Maps.DirectionFinder.Avoid.HIGHWAYS,
);

Parameters

NameTypeDescription
avoidStringA constant value from Avoid .

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.

See also


setDepart(time)

Sets the desired time of departure (when applicable).

// Creates a DirectionFinder with a departure time of 1 hour from now.
constnow=newDate();
constdepart=newDate(now.getTime()+1*60*60*1000);
constdirectionFinder=Maps.newDirectionFinder().setDepart(depart);

Parameters

NameTypeDescription
timeDateThe time of departure.

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.

See also


setDestination(latitude, longitude)

Sets the ending location for which to calculate directions to, using a point (lat/lng).

// Creates a DirectionFinder with the destination set to Central Park.
constdirectionFinder=Maps.newDirectionFinder().setDestination(
40.777052,
-73.975464,
);

Parameters

NameTypeDescription
latitudeNumberThe latitude of the ending location.
longitudeNumberThe longitude of the ending location.

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.


setDestination(address)

Sets the ending location for which to calculate directions to, using an address.

// Creates a DirectionFinder with the destination set to Central Park.
constdirectionFinder=Maps.newDirectionFinder().setDestination(
'Central Park, New York, NY',
);

Parameters

NameTypeDescription
addressStringThe ending address.

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.


setLanguage(language)

Sets the language to be used for the directions.

// Creates a DirectionFinder with the language set to French.
constdirectionFinder=Maps.newDirectionFinder().setLanguage('fr');

Parameters

NameTypeDescription
languageStringA BCP-47 language identifier.

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.

See also


setMode(mode)

Sets the mode of travel (defaults to driving).

// Creates a DirectionFinder with the mode set to walking.
constdirectionFinder=Maps.newDirectionFinder().setMode(
Maps.DirectionFinder.Mode.WALKING,
);

Parameters

NameTypeDescription
modeStringA constant value from Mode .

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.

See also


setOptimizeWaypoints(optimizeOrder)

Sets whether or not to optimize the provided route by rearranging the waypoints in a more efficient order (defaults to false).

// Creates a DirectionFinder with wapoint optimization enabled.
constdirectionFinder=Maps.newDirectionFinder().setOptimizeWaypoints(true);

Parameters

NameTypeDescription
optimizeOrderBooleantrue to optimize the order, or false otherwise.

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.

See also


setOrigin(latitude, longitude)

Sets the starting location from which to calculate directions, using a point (lat/lng).

// Creates a DirectionFinder with the origin set to Times Square.
constdirectionFinder=Maps.newDirectionFinder().setOrigin(
40.759011,
-73.984472,
);

Parameters

NameTypeDescription
latitudeNumberThe latitude of the starting location.
longitudeNumberThe longitude of the starting location.

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.


setOrigin(address)

Sets the starting location from which to calculate directions, using an address.

// Creates a DirectionFinder with the origin set to Times Square.
constdirectionFinder=Maps.newDirectionFinder().setOrigin(
'Times Square, New York, NY',
);

Parameters

NameTypeDescription
addressStringThe starting address.

Return

DirectionFinder — The DirectionFinder instance to facilitate chaining of calls.


setRegion(region)

Sets a region to use when interpreting location names. The supported region codes correspond to the ccTLDs supported by Google Maps. For example, the region code "uk" corresponds to "maps.google.co.uk".

// Creates a DirectionFinder with the region set to France.
constdirectionFinder=Maps.newDirectionFinder().setRegion('fr');

Parameters

NameTypeDescription
regionStringThe region code to use.

Return

DirectionFinder — The DirectionFinder object to facilitate chaining of calls.

See also

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 2026年04月13日 UTC.