Nhận thông tin tuyến đường

Hãy làm theo hướng dẫn này để thiết lập ứng dụng nhằm truy xuất thời gian, khoảng cách và các chặng trong tuyến đường hiện tại.

Tổng quan

Để lấy thông tin về tuyến đường hiện tại, hãy lấy thuộc tính phù hợp từ thực thể navigator:

Xem mã

Đang đến điểm đến tiếp theo

Để biết thời gian đến điểm đến tiếp theo, hãy gọi timeToNextDestination(). Thao tác này sẽ trả về giá trị NSTimeInterval. Ví dụ sau đây cho thấy việc ghi nhật ký thời gian cho đích đến tiếp theo:

Swift

if let navigator = mapView.navigator {
  let time = navigator.timeToNextDestination
  let minutes = floor(time/60)
  let seconds = round(time - minutes * 60)
  NSLog("Time to next destination: %.0f:%.0f", minutes, seconds)
}

Objective-C

NSTimeInterval time = _mapView.navigator.timeToNextDestination;
int minutes = floor(time/60);
int seconds = round(time - minutes * 60);
NSLog(@"%@", [NSString stringWithFormat:@"Time to next destination: %i:%i.", minutes, seconds]);

Đang nhận khoảng cách đến điểm đến tiếp theo

Để biết khoảng cách đến điểm đến tiếp theo, hãy gọi distanceToNextDestination(). Thao tác này sẽ trả về giá trị CLLocationDistance. Đơn vị được chỉ định bằng mét.

Swift

if let navigator = mapView.navigator {
  let distance = navigator.distanceToNextDestination
  let miles = distance * 0.00062137
  NSLog("Distance to next destination: %.2f miles.", miles)
}

Objective-C

CLLocationDistance distance = _mapView.navigator.distanceToNextDestination;
double miles = distance * 0.00062137;
NSLog(@"%@", [NSString stringWithFormat:@"Distance to next destination: %.2f.", miles]);

Đang tải thông tin về tình trạng giao thông cho điểm đến tiếp theo

Để nhận một giá trị cho biết luồng lưu lượng truy cập tới đích đến tiếp theo, hãy gọi delayCategoryToNextDestination. Tham số này trả về GMSNavigationDelayCategory. Ví dụ sau đây cho thấy cách đánh giá kết quả và ghi nhật ký một thông báo lưu lượng truy cập:

Swift

if let navigator = mapView.navigator {
  // insert sample for evaluating traffic value
  let delay = navigator.delayCategoryToNextDestination
  let traffic = "unavailable"
  switch delay {
    case .noData:
      traffic = "unavailable"
    case .heavy:
      traffic = "heavy"
    case .medium:
      traffic = "moderate"
    case .light:
      traffic = "light"
    default:
      traffic = "unavailable"
  }
  print("Traffic is \(traffic).")
}

Objective-C

GMSNavigationDelayCategory delay = mapView.navigator.delayCategoryToNextDestination;
NSString *traffic = @"";

switch (delayCategory) {
    case GMSNavigationDelayCategoryNoData:
      traffic = @"No Data";
      break;
    case GMSNavigationDelayCategoryHeavy:
      traffic = @"Heavy";
      break;
    case GMSNavigationDelayCategoryMedium:
      traffic = @"Medium";
      break;
    case GMSNavigationDelayCategoryLight:
      traffic = @"Light";
      break;
    default:
      NSLog(@"Invalid delay category: %zd", delayCategory);
 }

NSLog(@"%@", [NSString stringWithFormat:@"Traffic is %@.", traffic]);

Đang tải thông tin về chân hiện tại

Để nhận thông tin về chặng hiện tại của tuyến đường, hãy gọi currentRouteLeg. Thao tác này sẽ trả về một thực thể GMSRouteLeg, từ đó bạn có thể lấy:

  • Điểm đến cho chân.
  • Toạ độ cuối cùng ở chân.
  • Đường dẫn chứa toạ độ tạo nên chân tuyến đường.

Ví dụ sau đây cho thấy việc ghi nhật ký tiêu đề và toạ độ vĩ độ/lng cho chặng tiếp theo của tuyến đường:

Swift

if let navigator = mapView.navigator {
  let currentLeg = navigator.currentRouteLeg
  let nextDestination = currentLeg?.destinationWaypoint?.title
  let lat = currentLeg?.destinationCoordinate.latitude.description
  let lng = currentLeg?.destinationCoordinate.longitude.description
  NSLog(nextDestination! + ", " + lat! + "/" + lng!)
}

Objective-C

GMSRouteLeg *currentSegment = _mapView.navigator.currentRouteLeg;
NSString *nextDestination = currentSegment.destinationWaypoint.title;
CLLocationDegrees lat = currentSegment.destinationCoordinate.latitude;
CLLocationDegrees lng = currentSegment.destinationCoordinate.longitude;
NSLog(@"%@", [NSString stringWithFormat:@"%@, %f/%f", nextDestination, lat, lng]);

Đang tải con đường được đi gần đây nhất

Để xem lộ trình mà bạn đã đi gần đây nhất, hãy gọi traveledPath. Thao tác này sẽ trả về một thực thể GMSPath đã được đơn giản hoá để xoá các điểm thừa (ví dụ: chuyển các điểm cộng tuyến tính liên tiếp thành một đoạn thẳng). Đường dẫn này trống cho đến khi bắt đầu hướng dẫn. Ví dụ sau đây cho thấy cách lấy đường dẫn đã đi gần đây nhất:

Swift

if let navigator = mapView.navigator {
  let latestPath = navigator.traveledPath
  if latestPath.count() > 0 {
    let begin: CLLocationCoordinate2D = latestPath.coordinate(at: 0)
    let current: CLLocationCoordinate2D = latestPath.coordinate(at: latestPath.count() - 1)
    print("Path from (\(begin.latitude),\(begin.longitude)) to (\(current.latitude),\(current.longitude))")
  }
}

Objective-C

GMSPath *latestPath = mapView.navigator.traveledPath;
if (latestPath.count > 0) {
  CLLocationCoordinate2D begin = [latestPath coordinateAtIndex:0];
  CLLocationCoordinate2D current = [latestPath coordinateAtIndex:latestPath.count - 1];
  NSLog(@"Path from %f/%f to %f/%f",
        begin.latitude,
        begin.longitude,
        current.latitude,
        current.longitude);
}