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เพิ่มจุดอ้างอิงที่เส้นทางต้องผ่านโดยใช้จุด (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.
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,
);

พารามิเตอร์

ชื่อประเภทคำอธิบาย
avoidStringค่าคงที่จาก Avoid

รีเทิร์น

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)

กำหนดสถานที่สิ้นสุดที่จะคำนวณเส้นทางโดยใช้จุด (lat/lng)

// 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');

พารามิเตอร์

ชื่อประเภทคำอธิบาย
languageStringตัวระบุภาษา BCP-47

รีเทิร์น

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

ดูเพิ่มเติม


setMode(mode)

ตั้งค่าโหมดการเดินทาง (ค่าเริ่มต้นคือการขับรถ)

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

พารามิเตอร์

ชื่อประเภทคำอธิบาย
modeStringค่าคงที่จาก Mode

รีเทิร์น

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)

กำหนดตำแหน่งเริ่มต้นที่จะใช้คำนวณเส้นทางโดยใช้จุด (lat/lng)

// 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)

ตั้งค่าภูมิภาคที่จะใช้เมื่อตีความชื่อสถานที่ รหัสภูมิภาคที่รองรับจะสอดคล้องกับ ccTLD ที่ Google Maps รองรับ เช่น รหัสภูมิภาค "uk" สอดคล้องกับ "maps.google.co.uk"

// Creates a DirectionFinder with the region set to France.
const directionFinder = Maps.newDirectionFinder().setRegion('fr');

พารามิเตอร์

ชื่อประเภทคำอธิบาย
regionStringรหัสภูมิภาคที่จะใช้

รีเทิร์น

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

ดูเพิ่มเติม