可擷取地點之間的路線。
以下範例說明如何使用這個類別取得從 Times Square 到中央公園的路線,首先在 Lincoln Center 停靠點,在地圖上繪製地點和路徑,然後透過電子郵件將地圖傳送到這裡。
// 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 | 使用點 (lat/lng) 設定用於計算路線的結束位置。 |
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)
使用點 (lat/lng) 新增路線必須通過的路線控點。
// Creates a DirectionFinder with a wapoint at Lincoln Center. var directionFinder = Maps.newDirectionFinder().addWaypoint(40.772628, -73.984243);
參數
名稱 | 類型 | 說明 |
---|---|---|
latitude | Number | 路線控點的緯度。 |
longitude | Number | 路線控點的經度。 |
Return 鍵
DirectionFinder
— DirectionFinder 物件,用於鏈結呼叫鏈。
addWaypoint(address)
使用地址新增路線必須通過的路線控點。
// Creates a DirectionFinder with a wapoint at Lincoln Center. var directionFinder = Maps.newDirectionFinder().addWaypoint('Lincoln Center, New York, NY');
參數
名稱 | 類型 | 說明 |
---|---|---|
address | String | 地址。 |
Return 鍵
DirectionFinder
— DirectionFinder 物件,用於鏈結呼叫鏈。
clearWaypoints()
清除目前的路線控點。
var directionFinder = Maps.newDirectionFinder() // ... // Do something interesting here ... // ... // Remove all waypoints added with addWaypoint(). directionFinder.clearWaypoints();
Return 鍵
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);
Return 鍵
Object
— 包含路線規劃集的 JSON 物件,如這裡所述
另請參閱
setAlternatives(useAlternatives)
設定是否應傳回替代路徑,而非只有最高排名路徑 (預設為 false)。如為 true,產生的物件和 routes
陣列中可能包含多個項目。
// Creates a DirectionFinder with alernative routes enabled. var directionFinder = Maps.newDirectionFinder().setAlternatives(true);
參數
名稱 | 類型 | 說明 |
---|---|---|
useAlternatives | Boolean | 傳回 True 則傳回其他路徑,否則傳回 false |
Return 鍵
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);
參數
名稱 | 類型 | 說明 |
---|---|---|
time | Date | 抵達時間 |
Return 鍵
DirectionFinder
:DirectionFinder 物件,用於鏈結呼叫鏈
另請參閱
setAvoid(avoid)
設定是否要避免特定類型的限制。
// Creates a DirectionFinder that avoid highways. var directionFinder = Maps.newDirectionFinder().setAvoid(Maps.DirectionFinder.Avoid.HIGHWAYS);
參數
名稱 | 類型 | 說明 |
---|---|---|
avoid | String | 來自 Avoid 的常數值 |
Return 鍵
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);
參數
名稱 | 類型 | 說明 |
---|---|---|
time | Date | 出發時間 |
Return 鍵
DirectionFinder
— DirectionFinder 物件,用於鏈結呼叫鏈。
另請參閱
setDestination(latitude, longitude)
使用點 (lat/lng) 設定用於計算路線的結束位置。
// Creates a DirectionFinder with the destination set to Central Park. var directionFinder = Maps.newDirectionFinder().setDestination(40.777052, -73.975464);
參數
名稱 | 類型 | 說明 |
---|---|---|
latitude | Number | 終點位置的緯度 |
longitude | Number | 終點位置的經度 |
Return 鍵
DirectionFinder
:DirectionFinder 物件,用於鏈結呼叫鏈
setDestination(address)
使用地址設定要用來計算路線的終點。
// Creates a DirectionFinder with the destination set to Central Park. var directionFinder = Maps.newDirectionFinder().setDestination('Central Park, New York, NY');
參數
名稱 | 類型 | 說明 |
---|---|---|
address | String | 結尾地址 |
Return 鍵
DirectionFinder
:DirectionFinder 物件,用於鏈結呼叫鏈
setLanguage(language)
設定路線要使用的語言。
// Creates a DirectionFinder with the language set to French. var directionFinder = Maps.newDirectionFinder().setLanguage('fr');
參數
名稱 | 類型 | 說明 |
---|---|---|
language | String | BCP-47 語言 ID |
Return 鍵
DirectionFinder
:DirectionFinder 物件,用於鏈結呼叫鏈
另請參閱
setMode(mode)
設定交通方式 (預設為開車)。
// Creates a DirectionFinder with the mode set to walking. var directionFinder = Maps.newDirectionFinder().setMode(Maps.DirectionFinder.Mode.WALKING);
參數
名稱 | 類型 | 說明 |
---|---|---|
mode | String | 來自 Mode 的常數值 |
Return 鍵
DirectionFinder
:DirectionFinder 物件,用於鏈結呼叫鏈
另請參閱
setOptimizeWaypoints(optimizeOrder)
設定是否要以更有效率的順序重新排列路線控點 (預設為 false),設定是否要將所提供的路線最佳化。
// Creates a DirectionFinder with wapoint optimization enabled. var directionFinder = Maps.newDirectionFinder().setOptimizeWaypoints(true);
參數
名稱 | 類型 | 說明 |
---|---|---|
optimizeOrder | Boolean | True 以最佳化訂單,否則為 False |
Return 鍵
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);
參數
名稱 | 類型 | 說明 |
---|---|---|
latitude | Number | 起始位置的緯度 |
longitude | Number | 起始位置的經度 |
Return 鍵
DirectionFinder
:DirectionFinder 物件,用於鏈結呼叫鏈
setOrigin(address)
使用地址設定用來計算路線的起點。
// Creates a DirectionFinder with the origin set to Times Square. var directionFinder = Maps.newDirectionFinder().setOrigin('Times Square, New York, NY');
參數
名稱 | 類型 | 說明 |
---|---|---|
address | String | 起始地址 |
Return 鍵
DirectionFinder
:用於引導呼叫的 DirectionFinder 執行個體
setRegion(region)
設定在解讀位置名稱時要使用的區域。支援的區域代碼對應到 Google 地圖支援的 ccTLD。例如,區碼「quot;uk"」對應於 &maps.google.co.uk"。
// Creates a DirectionFinder with the region set to France. var directionFinder = Maps.newDirectionFinder().setRegion('fr');
參數
名稱 | 類型 | 說明 |
---|---|---|
region | String | 要使用的區域代碼 |
Return 鍵
DirectionFinder
:DirectionFinder 物件,用於鏈結呼叫鏈