允許擷取地點之間的路線。
以下範例說明如何使用這個類別取得從時代廣場到中央公園的路線,先從 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 | 使用一個點 (經緯度) 新增路線必須通過的路線控點。 |
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)
使用點 (lat/lng) 新增路線必須通過的路線控點。
// Creates a DirectionFinder with a wapoint at Lincoln Center. var directionFinder = Maps.newDirectionFinder().addWaypoint(40.772628, -73.984243);
參數
名稱 | 類型 | 說明 |
---|---|---|
latitude | Number | 路線控點的緯度。 |
longitude | Number | 路線控點的經度。 |
回攻員
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 | 地址。 |
回攻員
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);
參數
名稱 | 類型 | 說明 |
---|---|---|
useAlternatives | Boolean | 傳回 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);
參數
名稱 | 類型 | 說明 |
---|---|---|
time | Date | 抵達時間 |
回攻員
DirectionFinder
:DirectionFinder 物件用於鏈結呼叫
另請參閱
setAvoid(avoid)
設定是否要避免特定類型的限制。
// Creates a DirectionFinder that avoid highways. var directionFinder = Maps.newDirectionFinder().setAvoid(Maps.DirectionFinder.Avoid.HIGHWAYS);
參數
名稱 | 類型 | 說明 |
---|---|---|
avoid | String | 來自 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);
參數
名稱 | 類型 | 說明 |
---|---|---|
time | Date | 出發時間 |
回攻員
DirectionFinder
— DirectionFinder 物件可用於鏈結呼叫。
另請參閱
setDestination(latitude, longitude)
設定要計算路線的終點位置,並使用點 (經緯度)。
// Creates a DirectionFinder with the destination set to Central Park. var directionFinder = Maps.newDirectionFinder().setDestination(40.777052, -73.975464);
參數
名稱 | 類型 | 說明 |
---|---|---|
latitude | Number | 終點位置的緯度 |
longitude | Number | 終點位置的經度 |
回攻員
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 | 結束地址 |
回攻員
DirectionFinder
:DirectionFinder 物件用於鏈結呼叫
setLanguage(language)
設定路線要使用的語言。
// Creates a DirectionFinder with the language set to French. var directionFinder = Maps.newDirectionFinder().setLanguage('fr');
參數
名稱 | 類型 | 說明 |
---|---|---|
language | String | BCP-47 語言 ID |
回攻員
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 的常數值 |
回攻員
DirectionFinder
:DirectionFinder 物件用於鏈結呼叫
另請參閱
setOptimizeWaypoints(optimizeOrder)
設定是否以更有效率的順序重新排列路線控點,藉此最佳化提供的路線 (預設值為 false)。
// Creates a DirectionFinder with wapoint optimization enabled. var directionFinder = Maps.newDirectionFinder().setOptimizeWaypoints(true);
參數
名稱 | 類型 | 說明 |
---|---|---|
optimizeOrder | Boolean | true 以將訂單最佳化,否則傳回 false |
回攻員
DirectionFinder
:DirectionFinder 物件用於鏈結呼叫
另請參閱
setOrigin(latitude, longitude)
設定用來計算路線的起點 (經緯度)。
// Creates a DirectionFinder with the origin set to Times Square. var directionFinder = Maps.newDirectionFinder().setOrigin(40.759011, -73.984472);
參數
名稱 | 類型 | 說明 |
---|---|---|
latitude | Number | 起點的緯度 |
longitude | Number | 起點的經度 |
回攻員
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 | 起始地址 |
回攻員
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');
參數
名稱 | 類型 | 說明 |
---|---|---|
region | String | 要使用的區碼 |
回攻員
DirectionFinder
:DirectionFinder 物件用於鏈結呼叫