Class DirectionFinder

Công cụtìm đường

Cho phép truy xuất đường đi giữa các vị trí.
Ví dụ bên dưới cho thấy cách bạn có thể sử dụng lớp này để nhận chỉ đường từ Quảng trường Thời đại đến Công viên Trung tâm, dừng chân tại Trung tâm Lincoln, vẽ các vị trí và đường đi trên bản đồ, đồng thời gửi bản đồ đó qua email.

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

Xem thêm

Phương thức

Phương thứcKiểu dữ liệu trả vềMô tả ngắn
addWaypoint(latitude, longitude)DirectionFinderThêm một điểm tham chiếu mà tuyến đường phải đi qua, bằng cách sử dụng một điểm (lat/lng).
addWaypoint(address)DirectionFinderThêm một điểm tham chiếu mà tuyến đường phải đi qua, bằng cách sử dụng địa chỉ.
clearWaypoints()DirectionFinderXoá bộ điểm tham chiếu hiện tại.
getDirections()ObjectLấy chỉ đường bằng cách sử dụng điểm bắt đầu, điểm đến và các lựa chọn khác đã được đặt.
setAlternatives(useAlternatives)DirectionFinderĐặt xem có nên trả về các tuyến đường thay thế hay không, thay vì chỉ trả về tuyến đường được xếp hạng cao nhất (mặc định là false).
setArrive(time)DirectionFinderĐặt thời gian đến dự kiến (nếu có).
setAvoid(avoid)DirectionFinderĐặt xem có tránh một số loại quy định hạn chế hay không.
setDepart(time)DirectionFinderĐặt thời gian khởi hành mong muốn (nếu có).
setDestination(latitude, longitude)DirectionFinderĐặt vị trí kết thúc để tính toán đường đi, bằng cách sử dụng một điểm (lat/lng).
setDestination(address)DirectionFinderĐặt vị trí kết thúc để tính toán đường đi, bằng cách sử dụng một địa chỉ.
setLanguage(language)DirectionFinderĐặt ngôn ngữ sẽ dùng cho chỉ đường.
setMode(mode)DirectionFinderĐặt chế độ di chuyển (mặc định là lái xe).
setOptimizeWaypoints(optimizeOrder)DirectionFinderĐặt xem có tối ưu hoá tuyến đường đã cung cấp bằng cách sắp xếp lại các điểm tham chiếu theo thứ tự hiệu quả hơn hay không (mặc định là false).
setOrigin(latitude, longitude)DirectionFinderĐặt vị trí bắt đầu để tính toán đường đi, bằng cách sử dụng một điểm (lat/lng).
setOrigin(address)DirectionFinderĐặt vị trí bắt đầu để tính toán đường đi bằng cách sử dụng địa chỉ.
setRegion(region)DirectionFinderĐặt một khu vực để sử dụng khi diễn giải tên vị trí.

Tài liệu chi tiết

addWaypoint(latitude, longitude)

Thêm một điểm tham chiếu mà tuyến đường phải đi qua, bằng cách sử dụng một điểm (lat/lng).

// Creates a DirectionFinder with a wapoint at Lincoln Center.
const directionFinder = Maps.newDirectionFinder().addWaypoint(
    40.772628,
    -73.984243,
);

Thông số

TênLoạiMô tả
latitudeNumberVĩ độ của điểm đánh dấu.
longitudeNumberKinh độ của điểm tham chiếu.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.


addWaypoint(address)

Thêm một điểm tham chiếu mà tuyến đường phải đi qua, bằng cách sử dụng địa chỉ.

// Creates a DirectionFinder with a wapoint at Lincoln Center.
const directionFinder = Maps.newDirectionFinder().addWaypoint(
    'Lincoln Center, New York, NY',
);

Thông số

TênLoạiMô tả
addressStringMột địa chỉ.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.


clearWaypoints()

Xoá bộ điểm tham chiếu hiện tại.

const directionFinder = Maps.newDirectionFinder();
// ...
// Do something interesting here ...
// ...
// Remove all waypoints added with addWaypoint().
directionFinder.clearWaypoints();

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.


getDirections()

Lấy chỉ đường bằng cách sử dụng điểm bắt đầu, điểm đến và các lựa chọn khác đã được đặt.

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

Cầu thủ trả bóng

Object – Một đối tượng JSON chứa tập hợp các tuyến đường cho chỉ đường, như mô tả tại đây.

Xem thêm


setAlternatives(useAlternatives)

Đặt xem có nên trả về các tuyến đường thay thế hay không, thay vì chỉ trả về tuyến đường được xếp hạng cao nhất (mặc định là false). Nếu true, mảng routes của đối tượng kết quả có thể chứa nhiều mục.

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

Thông số

TênLoạiMô tả
useAlternativesBooleantrue để trả về các tuyến đường thay thế, nếu không thì false.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.


setArrive(time)

Đặt thời gian đến dự kiến (nếu có).

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

Thông số

TênLoạiMô tả
timeDateThời gian đến.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.

Xem thêm


setAvoid(avoid)

Đặt xem có tránh một số loại quy định hạn chế hay không.

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

Thông số

TênLoạiMô tả
avoidStringMột giá trị hằng số từ Avoid.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.

Xem thêm


setDepart(time)

Đặt thời gian khởi hành mong muốn (nếu có).

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

Thông số

TênLoạiMô tả
timeDateThời gian khởi hành.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.

Xem thêm


setDestination(latitude, longitude)

Đặt vị trí kết thúc để tính toán đường đi, bằng cách sử dụng một điểm (lat/lng).

// Creates a DirectionFinder with the destination set to Central Park.
const directionFinder = Maps.newDirectionFinder().setDestination(
    40.777052,
    -73.975464,
);

Thông số

TênLoạiMô tả
latitudeNumberVĩ độ của vị trí kết thúc.
longitudeNumberKinh độ của vị trí kết thúc.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.


setDestination(address)

Đặt vị trí kết thúc để tính toán đường đi, bằng cách sử dụng một địa chỉ.

// Creates a DirectionFinder with the destination set to Central Park.
const directionFinder = Maps.newDirectionFinder().setDestination(
    'Central Park, New York, NY',
);

Thông số

TênLoạiMô tả
addressStringĐịa chỉ kết thúc.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.


setLanguage(language)

Đặt ngôn ngữ sẽ dùng cho chỉ đường.

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

Thông số

TênLoạiMô tả
languageStringGiá trị nhận dạng ngôn ngữ BCP-47.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.

Xem thêm


setMode(mode)

Đặt chế độ di chuyển (mặc định là lái xe).

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

Thông số

TênLoạiMô tả
modeStringMột giá trị hằng số từ Mode.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.

Xem thêm


setOptimizeWaypoints(optimizeOrder)

Đặt xem có tối ưu hoá tuyến đường đã cung cấp bằng cách sắp xếp lại các điểm tham chiếu theo thứ tự hiệu quả hơn hay không (mặc định là false).

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

Thông số

TênLoạiMô tả
optimizeOrderBooleantrue để tối ưu hoá thứ tự hoặc false nếu không.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.

Xem thêm


setOrigin(latitude, longitude)

Đặt vị trí bắt đầu để tính toán đường đi, bằng cách sử dụng một điểm (lat/lng).

// Creates a DirectionFinder with the origin set to Times Square.
const directionFinder = Maps.newDirectionFinder().setOrigin(
    40.759011,
    -73.984472,
);

Thông số

TênLoạiMô tả
latitudeNumberVĩ độ của vị trí bắt đầu.
longitudeNumberKinh độ của vị trí bắt đầu.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.


setOrigin(address)

Đặt vị trí bắt đầu để tính toán đường đi bằng cách sử dụng địa chỉ.

// Creates a DirectionFinder with the origin set to Times Square.
const directionFinder = Maps.newDirectionFinder().setOrigin(
    'Times Square, New York, NY',
);

Thông số

TênLoạiMô tả
addressStringĐịa chỉ bắt đầu.

Cầu thủ trả bóng

DirectionFinder – Thực thể DirectionFinder để tạo điều kiện cho việc liên kết các lệnh gọi.


setRegion(region)

Đặt một khu vực để sử dụng khi diễn giải tên vị trí. Mã vùng được hỗ trợ tương ứng với ccTLD mà Google Maps hỗ trợ. Ví dụ: mã khu vực "uk" tương ứng với "maps.google.co.uk".

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

Thông số

TênLoạiMô tả
regionStringMã khu vực cần sử dụng.

Cầu thủ trả bóng

DirectionFinder – Đối tượng DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi.

Xem thêm