获取路线信息

按照本指南设置您的应用,以检索当前路线的行程时间、距离和路线分段。

概览

如需获取有关当前路线的信息,请从 navigator 实例中获取相应的属性:

查看代码

获取到达下一个目的地所需时间

如需获取到达下一个目的地所需的时间,请调用 timeToNextDestination()。这会返回一个 NSTimeInterval 值。以下示例展示了如何记录到达下一个目的地的时间:

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

获取到达下一个目的地的距离

如需获取到达下一个目的地的距离,请调用 distanceToNextDestination()。这将返回一个 CLLocationDistance 值。单位以米为单位。

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

获取前往下一个目的地的路况信息

如需获取指示流量流向下一个目的地的值,请调用 delayCategoryToNextDestination。此参数会返回 GMSNavigationDelayCategory。以下示例展示了如何评估结果并记录流量消息:

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

获取有关当前航段的信息

如需获取当前路线分段的相关信息,请调用 currentRouteLeg。这会返回一个 GMSRouteLeg 实例,您可以从中获取:

  • 相应航段的目的地。
  • 相应路段中的最后一个坐标。
  • 包含构成路线航段的坐标的路径。

以下示例展示了如何记录下一条路线的标题和经纬度坐标:

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

获取最近一次行经的路径

如需获取最近行过的路径,请调用 traveledPath。这会返回一个经过简化以移除多余点的 GMSPath 实例(例如,将连续的共线点转换为单个线段)。在开始引导之前,此路径为空。以下示例展示了如何获取最近行过的路径:

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