קבלת מידע על המסלול

במדריך הזה מוסבר איך להגדיר את האפליקציה כדי לאחזר זמנים, מרחקים ומסלול רגליים במסלול הנוכחי.

סקירה כללית

כדי לקבל מידע על המסלול הנוכחי, צריך לאחזר את המאפיין המתאים מ: המופע של 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);
}