ช่วยให้สามารถดึงข้อมูลเส้นทางระหว่างสถานที่ต่างๆ
ตัวอย่างด้านล่างแสดงวิธีใช้คลาสนี้เพื่อขอเส้นทางจากไทม์สแควร์ไปเซ็นทรัลพาร์ก โดยหยุดที่ 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 | กำหนดตำแหน่งเริ่มต้นในการคำนวณเส้นทาง โดยใช้จุด (ละติจูด/ลองจิจูด) |
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);
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
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');
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
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) หากเป็น "จริง" อาร์เรย์ routes
ของออบเจ็กต์ที่ได้อาจมีหลายรายการ
// Creates a DirectionFinder with alernative routes enabled. var directionFinder = Maps.newDirectionFinder().setAlternatives(true);
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
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);
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
time | Date | เวลาถึง |
รีเทิร์น
DirectionFinder
— ออบเจ็กต์ DirectionFinder เพื่ออำนวยความสะดวกในการเชื่อมโยงการเรียกใช้
ดูเพิ่มเติม
setAvoid(avoid)
ตั้งค่าว่าจะหลีกเลี่ยงข้อจำกัดบางประเภทหรือไม่
// Creates a DirectionFinder that avoid highways. var directionFinder = Maps.newDirectionFinder().setAvoid(Maps.DirectionFinder.Avoid.HIGHWAYS);
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
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);
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
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);
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
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');
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
address | String | ที่อยู่สุดท้าย |
รีเทิร์น
DirectionFinder
— ออบเจ็กต์ DirectionFinder เพื่ออำนวยความสะดวกในการเชื่อมโยงการเรียกใช้
setLanguage(language)
ตั้งค่าภาษาที่ใช้ในเส้นทาง
// Creates a DirectionFinder with the language set to French. var directionFinder = Maps.newDirectionFinder().setLanguage('fr');
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
language | String | ตัวระบุภาษา BCP-47 |
รีเทิร์น
DirectionFinder
— ออบเจ็กต์ DirectionFinder เพื่ออำนวยความสะดวกในการเชื่อมโยงการเรียกใช้
ดูเพิ่มเติม
setMode(mode)
ตั้งค่าโหมดการเดินทาง (ค่าเริ่มต้นคือขับรถ)
// Creates a DirectionFinder with the mode set to walking. var directionFinder = Maps.newDirectionFinder().setMode(Maps.DirectionFinder.Mode.WALKING);
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
mode | String | ค่าคงที่จาก Mode |
รีเทิร์น
DirectionFinder
— ออบเจ็กต์ DirectionFinder เพื่ออำนวยความสะดวกในการเชื่อมโยงการเรียกใช้
ดูเพิ่มเติม
setOptimizeWaypoints(optimizeOrder)
ตั้งค่าว่าจะเพิ่มประสิทธิภาพเส้นทางที่ระบุหรือไม่ โดยการจัดเรียงจุดอ้างอิงใหม่ตามลำดับที่มีประสิทธิภาพมากขึ้น (ค่าเริ่มต้นคือ false)
// Creates a DirectionFinder with wapoint optimization enabled. var directionFinder = Maps.newDirectionFinder().setOptimizeWaypoints(true);
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
optimizeOrder | Boolean | "จริง" เพื่อเพิ่มประสิทธิภาพคำสั่งซื้อ หรือไม่เช่นนั้น |
รีเทิร์น
DirectionFinder
— ออบเจ็กต์ DirectionFinder เพื่ออำนวยความสะดวกในการเชื่อมโยงการเรียกใช้
ดูเพิ่มเติม
setOrigin(latitude, longitude)
กำหนดตำแหน่งเริ่มต้นในการคำนวณเส้นทาง โดยใช้จุด (ละติจูด/ลองจิจูด)
// Creates a DirectionFinder with the origin set to Times Square. var directionFinder = Maps.newDirectionFinder().setOrigin(40.759011, -73.984472);
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
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');
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
address | String | ที่อยู่เริ่มต้น |
รีเทิร์น
DirectionFinder
— อินสแตนซ์ DirectionFinder เพื่ออำนวยความสะดวกในเชนการเรียกใช้
setRegion(region)
ตั้งค่าภูมิภาคที่จะใช้เมื่อแปลความหมายชื่อสถานที่ รหัสภูมิภาคที่รองรับจะสอดคล้องกับ ccTLD ที่ Google Maps รองรับ ตัวอย่างเช่น รหัสภูมิภาค "uk" สอดคล้องกับ "maps.google.co.uk"
// Creates a DirectionFinder with the region set to France. var directionFinder = Maps.newDirectionFinder().setRegion('fr');
พารามิเตอร์
ชื่อ | Type | คำอธิบาย |
---|---|---|
region | String | รหัสภูมิภาคที่จะใช้ |
รีเทิร์น
DirectionFinder
— ออบเจ็กต์ DirectionFinder เพื่ออำนวยความสะดวกในการเชื่อมโยงการเรียกใช้