Class DirectionFinder

方向查找器

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

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

// Set up marker styles.

let markerLetterCode = 'A'.charCodeAt();

// Add markers to the map.
const map = Maps.newStaticMap();
for (let i = 0; i < route.legs.length; i++) {
  const leg = 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.
const 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添加路线必须经过的途经点(使用点 [纬度/经度])。
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使用点(纬度/经度)设置计算路线的起始位置。
setOrigin(address)DirectionFinder使用地址设置计算路线的起始位置。
setRegion(region)DirectionFinder设置在解读位置名称时要使用的区域。

详细文档

addWaypoint(latitude, longitude)

添加路线必须经过的途经点(使用点 [纬度/经度])。

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

参数

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

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。


addWaypoint(address)

添加路线必须经过的途经点(使用地址)。

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

参数

名称类型说明
addressString地址。

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。


clearWaypoints()

清除当前的一组途经点。

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

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。


getDirections()

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

// Logs how long it takes to walk from Times Square to Central Park.
const 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 alternative routes enabled.
const directionFinder = Maps.newDirectionFinder().setAlternatives(true);

参数

名称类型说明
useAlternativesBooleantrue 以返回替代路线,否则返回 false

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。


setArrive(time)

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

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

参数

名称类型说明
timeDate到达时间。

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。

另请参阅


setAvoid(avoid)

设置是否避开某些类型的限制。

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

参数

名称类型说明
avoidStringAvoid 中的常量值。

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。

另请参阅


setDepart(time)

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

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

参数

名称类型说明
timeDate出发时间。

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。

另请参阅


setDestination(latitude, longitude)

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

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

参数

名称类型说明
latitudeNumber结束位置的纬度。
longitudeNumber结束位置的经度。

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。


setDestination(address)

使用地址设置要计算路线的结束位置。

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

参数

名称类型说明
addressString结束地址。

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。


setLanguage(language)

设置要用于路线的语言。

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

参数

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

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。

另请参阅


setMode(mode)

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

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

参数

名称类型说明
modeStringMode 中的常量值。

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。

另请参阅


setOptimizeWaypoints(optimizeOrder)

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

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

参数

名称类型说明
optimizeOrderBooleantrue 表示优化订单,否则为 false

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。

另请参阅


setOrigin(latitude, longitude)

使用点(纬度/经度)设置计算路线的起始位置。

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

参数

名称类型说明
latitudeNumber起始位置的纬度。
longitudeNumber起始位置的经度。

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。


setOrigin(address)

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

// Creates a DirectionFinder with the origin set to Times Square.
const 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.
const directionFinder = Maps.newDirectionFinder().setRegion('fr');

参数

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

返回

DirectionFinder - DirectionFinder 对象,用于简化调用链。

另请参阅