Uzyskiwanie informacji o trasie

Postępuj zgodnie z tym przewodnikiem, aby skonfigurować aplikację do pobierania czasów, odległości i odcinków bieżącej trasy.

Przegląd

Aby uzyskać informacje o bieżącej trasie, pobierz odpowiednią właściwość z instancji navigator:

Wyświetl kod

Sprawdzam czas dojazdu do następnego miejsca docelowego

Aby sprawdzić czas do następnego miejsca docelowego, wywołaj timeToNextDestination(). Zwraca wartość NSTimeInterval. Poniższy przykład pokazuje, jak rejestrować czas do następnego miejsca docelowego:

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

Pobieram odległość do następnego miejsca docelowego

Aby uzyskać odległość do następnego miejsca docelowego, wywołaj metodę distanceToNextDestination(). Zwraca wartość CLLocationDistance. Jednostki są podawane w metrach.

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

Pobieram informacje o warunkach na drodze do następnego miejsca docelowego

Aby uzyskać wartość wskazującą przepływ ruchu do następnego miejsca docelowego, wywołaj delayCategoryToNextDestination. Ten parametr zwraca GMSNavigationDelayCategory. Poniższy przykład pokazuje, jak ocenić wynik i zarejestrować komunikat o ruchu:

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

Uzyskiwanie informacji o bieżącym etapie

Aby uzyskać informacje o bieżącym odcinku trasy, wywołaj currentRouteLeg. Zwraca to instancję GMSRouteLeg, z której możesz uzyskać:

  • Miejsce docelowe etapu.
  • Ostatnia współrzędna na odcinku.
  • Ścieżka zawierająca współrzędne, które tworzą odcinek trasy.

W przykładzie poniżej pokazujemy, jak zarejestrować tytuł i współrzędne geograficzne następnego etapu trasy:

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

Pobieranie ostatnio przebytej ścieżki

Aby uzyskać ostatnio przebytą trasę, wywołaj traveledPath. Zwraca instancję GMSPath, która została uproszczona przez usunięcie zbędnych punktów (np. przekształcenie kolejnych punktów współliniowych w pojedynczy odcinek). Ta ścieżka jest pusta, dopóki nie rozpoczniesz wskazówek. Poniższy przykład pokazuje, jak uzyskać ostatnio przebytą ścieżkę:

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