Class DirectionFinder

DirectionFinder

ช่วยให้สามารถดึงข้อมูลเส้นทางระหว่างสถานที่ต่างๆ
ตัวอย่างด้านล่างแสดงวิธีใช้คลาสนี้เพื่อขอเส้นทางจากไทม์สแควร์ไปเซ็นทรัลพาร์ก โดยหยุดที่ 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คำอธิบาย
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');

พารามิเตอร์

ชื่อTypeคำอธิบาย
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) หากเป็น "จริง" อาร์เรย์ routes ของออบเจ็กต์ที่ได้อาจมีหลายรายการ

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
useAlternativesBooleantrue เพื่อส่งคืนเส้นทางอื่น ไม่เป็น 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คำอธิบาย
timeDateเวลาถึง

รีเทิร์น

DirectionFinder — ออบเจ็กต์ DirectionFinder เพื่ออำนวยความสะดวกในการเชื่อมโยงการเรียกใช้

ดูเพิ่มเติม


setAvoid(avoid)

ตั้งค่าว่าจะหลีกเลี่ยงข้อจำกัดบางประเภทหรือไม่

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
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);

พารามิเตอร์

ชื่อTypeคำอธิบาย
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);

พารามิเตอร์

ชื่อTypeคำอธิบาย
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');

พารามิเตอร์

ชื่อTypeคำอธิบาย
addressStringที่อยู่สุดท้าย

รีเทิร์น

DirectionFinder — ออบเจ็กต์ DirectionFinder เพื่ออำนวยความสะดวกในการเชื่อมโยงการเรียกใช้


setLanguage(language)

ตั้งค่าภาษาที่ใช้ในเส้นทาง

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
languageStringตัวระบุภาษา 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คำอธิบาย
modeStringค่าคงที่จาก Mode

รีเทิร์น

DirectionFinder — ออบเจ็กต์ DirectionFinder เพื่ออำนวยความสะดวกในการเชื่อมโยงการเรียกใช้

ดูเพิ่มเติม


setOptimizeWaypoints(optimizeOrder)

ตั้งค่าว่าจะเพิ่มประสิทธิภาพเส้นทางที่ระบุหรือไม่ โดยการจัดเรียงจุดอ้างอิงใหม่ตามลำดับที่มีประสิทธิภาพมากขึ้น (ค่าเริ่มต้นคือ false)

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

พารามิเตอร์

ชื่อTypeคำอธิบาย
optimizeOrderBoolean"จริง" เพื่อเพิ่มประสิทธิภาพคำสั่งซื้อ หรือไม่เช่นนั้น

รีเทิร์น

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คำอธิบาย
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');

พารามิเตอร์

ชื่อTypeคำอธิบาย
addressStringที่อยู่เริ่มต้น

รีเทิร์น

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คำอธิบาย
regionStringรหัสภูมิภาคที่จะใช้

รีเทิร์น

DirectionFinder — ออบเจ็กต์ DirectionFinder เพื่ออำนวยความสะดวกในการเชื่อมโยงการเรียกใช้

ดูเพิ่มเติม