Class DirectionFinder

DirectionFinder

Cho phép truy xuất chỉ đường 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 để xem đường đi từ Quảng trường Thời đại đến Công viên trung tâm, trước tiên, hãy dừng ở Lincoln Center, vẽ các vị trí và đường đi trên bản đồ, rồi gửi bản đồ qua email.

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

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, sử dụng một điểm (vĩ độ/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á tập hợp điểm tham chiếu hiện tại.
getDirections()ObjectXem đường đi bằng cách sử dụng điểm xuất phát, điểm đến và các tuỳ chọn khác đã đặt.
setAlternatives(useAlternatives)DirectionFinderĐặt các tuyến đường thay thế có được trả về hay không, thay vì chỉ tuyến đường được xếp hạng cao nhất (mặc định là sai).
setArrive(time)DirectionFinderĐặt thời gian đến mong muốn (nếu có).
setAvoid(avoid)DirectionFinderĐặt xem có tránh một số loại hạn chế nhất định 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 chỉ đường, sử dụng một điểm (vĩ độ/lng).
setDestination(address)DirectionFinderĐặt vị trí kết thúc để tính chỉ đường đến, sử dụng địa chỉ.
setLanguage(language)DirectionFinderĐặt ngôn ngữ dùng cho thông tin chỉ đường.
setMode(mode)DirectionFinderĐặt phương thức đi lại (mặc định là lái xe).
setOptimizeWaypoints(optimizeOrder)DirectionFinderĐặt xem có tối ưu hoá tuyến đã cung cấp hay không 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 (mặc định là sai).
setOrigin(latitude, longitude)DirectionFinderĐặt vị trí bắt đầu để tính toán chỉ đường, sử dụng điểm (vĩ độ/lng).
setOrigin(address)DirectionFinderĐặt vị trí bắt đầu để tính toán chỉ đường, sử dụng địa chỉ.
setRegion(region)DirectionFinderĐặ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, sử dụng một điểm (vĩ độ/lng).

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

Các tham số

TênLoạiNội dung mô tả
latitudeNumberVĩ độ của điểm tham chiế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.
var directionFinder = Maps.newDirectionFinder().addWaypoint('Lincoln Center, New York, NY');

Các tham số

TênLoạiNội dung mô tả
addressStringĐị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á tập hợp điểm tham chiếu hiện tại.

var 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()

Xem đường đi bằng cách sử dụng điểm xuất phát, điểm đến và các tuỳ chọn khác đã đặt.

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

Cầu thủ trả bóng

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

Xem thêm


setAlternatives(useAlternatives)

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

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

Các tham số

TênLoạiNội dung mô tả
useAlternativesBooleantrue để trả về các tuyến thay thế, nếu không trả về 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 mong muốn (nếu có).

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

Các tham số

TênLoạiNội dung mô 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 hạn chế nhất định hay không.

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

Các tham số

TênLoạiNội dung mô tả
avoidStringmột giá trị không đổi 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.
var now = new Date();
var depart = new Date(now.getTime() + (1 * 60 * 60 * 1000));
var directionFinder = Maps.newDirectionFinder().setDepart(depart);

Các tham số

TênLoạiNội dung mô 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 chỉ đường, sử dụng một điểm (vĩ độ/lng).

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

Các tham số

TênLoạiNội dung mô 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 chỉ đường đến, sử dụng địa chỉ.

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

Các tham số

TênLoạiNội dung mô tả
addressStringđịa chỉ cuối

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ữ dùng cho thông tin chỉ đường.

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

Các tham số

TênLoạiNội dung mô tả
languageStringmã 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 phương thức đi lại (mặc định là lái xe).

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

Các tham số

TênLoạiNội dung mô tả
modeStringmột giá trị không đổi 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 đã cung cấp hay không 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 (mặc định là sai).

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

Các tham số

TênLoạiNội dung mô tả
optimizeOrderBooleantrue để tối ưu hoá đơn đặt hàng hoặc false nếu không muố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


setOrigin(latitude, longitude)

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

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

Các tham số

TênLoạiNội dung mô tả
latitudeNumbervĩ độ của vị trí xuất phát
longitudeNumberkinh độ của vị trí xuất phát

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 chỉ đường, sử dụng địa chỉ.

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

Các tham số

TênLoạiNội dung mô tả
addressStringđịa chỉ xuất phát

Cầu thủ trả bóng

DirectionFinder – thực thể DirectionFinder để hỗ trợ việc tạo chuỗi các lệnh gọi


setRegion(region)

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

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

Các tham số

TênLoạiNội dung mô tả
regionStringmã vùng cần 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