Class DirectionFinder

DirectionFinder

可用于检索位置之间的路线。
以下示例展示了如何使用此类获取从时代广场到中央公园的路线,先在林肯中心停车,在地图上绘制位置和路径,然后通过电子邮件发送地图。

// Get the directions.
var directions = 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();
var route = directions.routes[0];

// Set up marker styles.
var markerSize = Maps.StaticMap.MarkerSize.MID;
var markerColor = Maps.StaticMap.Color.GREEN
var markerLetterCode = 'A'.charCodeAt();

// Add markers to the map.
var map = Maps.newStaticMap();
for (var i = 0; i < route.legs.length; i++) {
  var leg = route.legs[i];
  if (i == 0) {
    // Add a marker for the start location of the first leg only.
    map.setMarkerStyle(markerSize, markerColor, String.fromCharCode(markerLetterCode));
    map.addMarker(leg.start_location.lat, leg.start_location.lng);
    markerLetterCode++;
  }
  map.setMarkerStyle(markerSize, markerColor, 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.
var toAddress = 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')
    }
  }
);

另请参阅

方法

方法返回类型简介
addWaypoint(latitude, longitude)DirectionFinder使用点 (lat/lng) 添加路线必须通过的航点。
addWaypoint(address)DirectionFinder使用地址添加路线必须通过的路标。
clearWaypoints()DirectionFinder清除当前的航点集。
getDirections()Object使用设置的出发地、目的地和其他设置选项获取路线。
setAlternatives(useAlternatives)DirectionFinder设置是否应返回备用路线,而不是仅返回排名最高的路线(默认为 false)。
setArrive(time)DirectionFinder设置所需的到达时间(如适用)。
setAvoid(avoid)DirectionFinder设置是否规避特定类型的限制。
setDepart(time)DirectionFinder设置所需的出发时间(如果适用)。
setDestination(latitude, longitude)DirectionFinder使用点(纬度/经度)设置要计算其路线的结束位置。
setDestination(address)DirectionFinder使用地址设置要计算其路线的结束位置。
setLanguage(language)DirectionFinder设置路线要使用的语言。
setMode(mode)DirectionFinder设置出行方式(默认为驾车)。
setOptimizeWaypoints(optimizeOrder)DirectionFinder通过以更高效的顺序(默认为 false)重新排列航点,设置是否优化提供的路线。
setOrigin(latitude, longitude)DirectionFinder使用点 (lat/lng) 设置用于计算路线的起始位置。
setOrigin(address)DirectionFinder使用地址设置用于计算路线的起始位置。
setRegion(region)DirectionFinder设置在解读位置名称时使用的区域。

详细文档

addWaypoint(latitude, longitude)

使用点(纬度/经度)添加路线必须通过的航点。

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

参数

名称类型说明
latitudeNumber航点的纬度。
longitudeNumber航点的经度。

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象。


addWaypoint(address)

使用地址添加路线必须通过的航点。

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

参数

名称类型说明
addressString地址。

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象。


clearWaypoints()

清除当前的航点集。

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

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象


getDirections()

使用设置的出发地、目的地和其他设置选项获取路线。

// Logs how long it would take to walk from Times Square to Central Park.
var directions = 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);

弃踢回攻

Object - 包含路线集的 JSON 对象,如此处所述

另请参阅


setAlternatives(useAlternatives)

设置是否应返回备用路线,而不是仅返回排名最高的路线(默认为 false)。如果为 true,所生成的对象的 routes 数组可以包含多个条目。

// Creates a DirectionFinder with alernative routes enabled.
var directionFinder = Maps.newDirectionFinder().setAlternatives(true);

参数

名称类型说明
useAlternativesBoolean返回 true 时返回备选路线,否则返回 false

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象


setArrive(time)

设置所需的到达时间(如适用)。

// Creates a DirectionFinder with an arrival time of 2 hours from now.
var now = new Date();
var arrive = new Date(now.getTime() + (2 * 60 * 60 * 1000));
var directionFinder = Maps.newDirectionFinder().setArrive(arrive);

参数

名称类型说明
timeDate到达时间

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象

另请参阅


setAvoid(avoid)

设置是否规避特定类型的限制。

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

参数

名称类型说明
avoidString来自 Avoid 的常量值

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象

另请参阅


setDepart(time)

设置所需的出发时间(如适用)。

// Creates a DirectionFinder with a departure time of 1 hour from now.
var now = new Date();
var depart = new Date(now.getTime() + (1 * 60 * 60 * 1000));
var directionFinder = Maps.newDirectionFinder().setDepart(depart);

参数

名称类型说明
timeDate出发时间

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象。

另请参阅


setDestination(latitude, longitude)

使用点(纬度/经度)设置要计算其路线的结束位置。

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

参数

名称类型说明
latitudeNumber终点位置的纬度
longitudeNumber终点位置的经度

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象


setDestination(address)

使用地址设置要计算其路线的终点位置。

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

参数

名称类型说明
addressString结束地址

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象


setLanguage(language)

设置路线要使用的语言。

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

参数

名称类型说明
languageStringBCP-47 语言标识符

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象

另请参阅


setMode(mode)

设置出行方式(默认为驾车)。

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

参数

名称类型说明
modeString来自 Mode 的常量值

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象

另请参阅


setOptimizeWaypoints(optimizeOrder)

通过以更高效的顺序(默认为 false)重新排列航点,设置是否优化提供的路线。

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

参数

名称类型说明
optimizeOrderBoolean如果为优化顺序,则为 true,否则为 false

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象

另请参阅


setOrigin(latitude, longitude)

使用点 (lat/lng) 设置用于计算路线的起始位置。

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

参数

名称类型说明
latitudeNumber出发地的纬度
longitudeNumber出发地的经度

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象


setOrigin(address)

使用地址设置用于计算路线的起始位置。

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

参数

名称类型说明
addressString出发地址

弃踢回攻

DirectionFinder - DirectionFinder 实例,用于简化调用链


setRegion(region)

设置在解读位置名称时使用的区域。支持的地区代码与 Google 地图支持的 ccTLD 相对应。例如,区域代码“uk”对应于“maps.google.co.uk”。

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

参数

名称类型说明
regionString要使用的区域代码

弃踢回攻

DirectionFinder - 用于简化调用链式的 DirectionFinder 对象

另请参阅