Ver informações do trajeto

Siga este guia para configurar seu app para recuperar horários, distâncias e trechos da rota atual.

Visão geral

Para receber informações sobre a rota atual, acesse a propriedade apropriada da instância navigator:

Achou o código?

Receber o tempo até o próximo destino

Para receber o tempo até o próximo destino, chame timeToNextDestination(). Isso retorna um valor NSTimeInterval. O exemplo a seguir mostra o registro do tempo até o próximo destino:

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

Receber a distância até o próximo destino

Para receber a distância até o próximo destino, chame distanceToNextDestination(). Isso retorna um valor CLLocationDistance. As unidades são especificadas em metros.

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

Receber as condições de trânsito até o próximo destino

Para receber um valor que indica o fluxo de trânsito até o próximo destino, chame delayCategoryToNextDestination. Esse parâmetro retorna GMSNavigationDelayCategory. O exemplo a seguir mostra a avaliação do resultado e o registro de uma mensagem de trânsito:

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

Receber informações sobre o trecho atual

Para receber informações sobre o trecho da rota atual, chame currentRouteLeg. Isso retorna uma instância GMSRouteLeg, da qual você pode receber:

  • O destino do trecho.
  • A coordenada final do trecho.
  • O caminho que contém as coordenadas que compõem o trecho da rota.

O exemplo a seguir mostra o registro do título e das coordenadas de latitude/longitude do próximo trecho da rota:

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

Receber o caminho percorrido mais recentemente

Para receber o caminho percorrido mais recentemente, chame traveledPath. Isso retorna uma GMSPath instância que foi simplificada para remover pontos redundantes (por exemplo, transformar pontos colineares consecutivos em um único segmento de linha). Esse caminho fica vazio até que a orientação seja iniciada. O exemplo a seguir mostra como receber o caminho percorrido mais recentemente:

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