경로 정보 가져오기

이 가이드에 따라 현재 경로의 시간, 거리, 경로 구간을 가져오도록 앱을 설정하세요.

개요

현재 경로에 관한 정보를 가져오려면 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);
}