اطلاعات مسیر را دریافت کنید

این راهنما را دنبال کنید تا برنامه خود را برای بازیابی زمان‌ها، مسافت‌ها و مسیرهای مسیر فعلی تنظیم کنید.

بررسی اجمالی

برای دریافت اطلاعات در مورد مسیر فعلی، ویژگی مناسب را از نمونه navigator دریافت کنید:

کد را ببینید

زمان رسیدن به مقصد بعدی

برای دریافت زمان به مقصد بعدی، timeToNextDestination() را فراخوانی کنید. این یک مقدار NSTimeInterval را برمی گرداند. مثال زیر ثبت زمان رسیدن به مقصد بعدی را نشان می دهد:

سریع

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

هدف-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 را برمی گرداند. واحدها بر حسب متر مشخص می شوند.

سریع

if let navigator = mapView.navigator {
  let distance = navigator.distanceToNextDestination
  let miles = distance * 0.00062137
  NSLog("Distance to next destination: %.2f miles.", miles)
}

هدف-C

CLLocationDistance distance = _mapView.navigator.distanceToNextDestination;
double miles = distance * 0.00062137;
NSLog(@"%@", [NSString stringWithFormat:@"Distance to next destination: %.2f.", miles]);

رسیدن به شرایط ترافیکی به مقصد بعدی

برای دریافت مقداری که جریان ترافیک به مقصد بعدی را نشان می‌دهد، delayCategoryToNextDestination را فراخوانی کنید. این پارامتر GMSNavigationDelayCategory را برمی گرداند. مثال زیر ارزیابی نتیجه و ثبت یک پیام ترافیک را نشان می دهد:

سریع

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).")
}

هدف-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 را برمی گرداند که از آن می توانید دریافت کنید:

  • مقصد برای پا.
  • مختصات نهایی در ساق پا.
  • مسیری که شامل مختصاتی است که پای مسیر را تشکیل می دهد.

مثال زیر ثبت مختصات عنوان و lat/lng را برای قسمت بعدی مسیر نشان می دهد:

سریع

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

هدف-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 را برمی گرداند که برای حذف نقاط اضافی ساده شده است (به عنوان مثال تبدیل نقاط خطی متوالی به یک پاره خط منفرد). این مسیر تا شروع راهنمایی خالی است. مثال زیر دریافت آخرین مسیر طی شده را نشان می دهد:

سریع

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

هدف-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);
}