الحصول على معلومات المسار

اتّبع هذا الدليل لإعداد تطبيقك لاسترداد الأوقات والمسافات وأجزاء المسار للمسار الحالي.

نظرة عامة

للحصول على معلومات عن المسار الحالي، احصل على السمة المناسبة من مثيل 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);
}