取得路線資訊

請按照這份指南的說明設定應用程式,以便擷取目前路線的時間、距離和路線路段。

總覽

如要取得目前路線的相關資訊,請從 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);
}