ルート情報を取得する

このガイドに沿って、現在のルートの時刻、距離、ルートの各区間を取得するアプリを設定します。

概要

現在のルートに関する情報を取得するには、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 インスタンスを返します(たとえば、連続する共線性ポイントを 1 つの線分に変換します)。このパスは、ガイダンスが開始されるまで空です。次の例は、最近使用した経路を取得する方法を示しています。

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