Ricevi informazioni sul percorso

Segui questa guida per configurare la tua app in modo da recuperare tempi, distanze e tappe del percorso corrente.

Panoramica

Per ottenere informazioni sul percorso corrente, recupera la proprietà appropriata dall'istanza navigator:

Visualizza il codice

Calcolo del tempo per raggiungere la destinazione successiva

Per ottenere il tempo necessario per raggiungere la destinazione successiva, chiama timeToNextDestination(). Viene restituito un valore NSTimeInterval. L'esempio seguente mostra la registrazione del tempo fino alla destinazione successiva:

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

Calcolo della distanza dalla destinazione successiva

Per ottenere la distanza dalla destinazione successiva, chiama distanceToNextDestination(). Viene restituito un valore CLLocationDistance. Le unità sono specificate in metri.

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

Recupero delle condizioni del traffico per la destinazione successiva in corso…

Per ottenere un valore che indica il flusso di traffico verso la destinazione successiva, chiama delayCategoryToNextDestination. Questo parametro restituisce GMSNavigationDelayCategory. L'esempio seguente mostra la valutazione del risultato e la registrazione di un messaggio sul traffico:

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

Ricevere informazioni sulla tappa corrente

Per ottenere informazioni sulla tappa del percorso corrente, chiama currentRouteLeg. Viene restituita un'istanza GMSRouteLeg, da cui puoi ottenere:

  • La destinazione della tappa.
  • La coordinata finale della tappa.
  • Il percorso contenente le coordinate che compongono la tappa del percorso.

L'esempio seguente mostra come registrare il titolo e le coordinate di latitudine/longitudine per la tappa successiva del percorso:

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

Recupero del percorso più recente

Per ottenere il percorso più recente, chiama traveledPath. Viene restituita un'istanza GMSPath che è stata semplificata per rimuovere i punti ridondanti (ad esempio, trasformando i punti colineari consecutivi in un unico segmento di linea). Questo percorso è vuoto finché non viene avviata la guida. L'esempio seguente mostra come ottenere il percorso più recente:

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