متابعة رحلة على جهاز iOS

اختيار النظام الأساسي: Android iOS JavaScript

عند تتبُّع رحلة، يعرض تطبيق المستهلك موقع المركبة المناسبة للمستهلك. لإجراء ذلك، يجب أن يبدأ تطبيقك في تتبُّع رحلة، وتعديل مستوى تقدّم الرحلة، وإيقاف تتبُّع الرحلة عند اكتمالها.

تتناول هذه المستندات كيفية عمل هذه العملية.

بدء تتبُّع رحلة

إليك كيفية بدء تتبُّع رحلة:

  • اجمَع جميع بيانات إدخال المستخدم، مثل مواقع التوصيل والاستلام، من ViewController.

  • أنشِئ ViewController جديدًا لبدء تتبُّع رحلة مباشرةً.

يوضّح المثال التالي كيفية بدء تتبُّع رحلة فور تحميل العرض.

Swift

/*
 * MapViewController.swift
 */
override func viewDidLoad() {
  super.viewDidLoad()
  ...
  self.mapView = GMTCMapView(frame: UIScreen.main.bounds)
  self.mapView.delegate = self
  self.view.addSubview(self.mapView)
}

func mapViewDidInitializeCustomerState(_: GMTCMapView) {
  self.mapView.pickupLocation = self.selectedPickupLocation
  self.mapView.dropoffLocation = self.selectedDropoffLocation

  self.startConsumerMatchWithLocations(
    pickupLocation: self.mapView.pickupLocation!,
    dropoffLocation: self.mapView.dropoffLocation!
  ) { [weak self] (tripName, error) in
    guard let strongSelf = self else { return }
    if error != nil {
      // print error message.
      return
    }
    let tripService = GMTCServices.shared().tripService
    // Create a tripModel instance for listening the update of the trip
    // specified by this trip name.
    let tripModel = tripService.tripModel(forTripName: tripName)
    // Create a journeySharingSession instance based on the tripModel
    let journeySharingSession = GMTCJourneySharingSession(tripModel: tripModel)
    // Add the journeySharingSession instance on the mapView for UI updating.
    strongSelf.mapView.show(journeySharingSession)
    // Register for the trip update events.
    tripModel.register(strongSelf)

    strongSelf.currentTripModel = tripModel
    strongSelf.currentJourneySharingSession = journeySharingSession
    strongSelf.hideLoadingView()
  }

  self.showLoadingView()
}

Objective-C

/*
 * MapViewController.m
 */
- (void)viewDidLoad {
  [super viewDidLoad];
  ...
  self.mapView = [[GMTCMapView alloc] initWithFrame:CGRectZero];
  self.mapView.delegate = self;
  [self.view addSubview:self.mapView];
}

// Handle the callback when the GMTCMapView did initialized.
- (void)mapViewDidInitializeCustomerState:(GMTCMapView *)mapview {
  self.mapView.pickupLocation = self.selectedPickupLocation;
  self.mapView.dropoffLocation = self.selectedDropoffLocation;

  __weak __typeof(self) weakSelf = self;
  [self startTripBookingWithPickupLocation:self.selectedPickupLocation
                           dropoffLocation:self.selectedDropoffLocation
                                completion:^(NSString *tripName, NSError *error) {
                                  __typeof(self) strongSelf = weakSelf;
                                  GMTCTripService *tripService = [GMTCServices sharedServices].tripService;
                                  // Create a tripModel instance for listening to updates to the trip specified by this trip name.
                                  GMTCTripModel *tripModel = [tripService tripModelForTripName:tripName];
                                  // Create a journeySharingSession instance based on the tripModel.
                                  GMTCJourneySharingSession *journeySharingSession =
                                    [[GMTCJourneySharingSession alloc] initWithTripModel:tripModel];
                                  // Add the journeySharingSession instance on the mapView for updating the UI.
                                  [strongSelf.mapView showMapViewSession:journeySharingSession];
                                  // Register for trip update events.
                                  [tripModel registerSubscriber:self];

                                  strongSelf.currentTripModel = tripModel;
                                  strongSelf.currentJourneySharingSession = journeySharingSession;
                                  [strongSelf hideLoadingView];
                                }];
    [self showLoadingView];
}

إيقاف تتبُّع رحلة

يمكنك إيقاف تتبُّع رحلة عند اكتمالها أو إلغائها. يوضّح المثال التالي كيفية إيقاف مشاركة الرحلة النشطة.

Swift

/*
 * MapViewController.swift
 */
func cancelCurrentActiveTrip() {
  // Stop the tripModel
  self.currentTripModel.unregisterSubscriber(self)

  // Remove the journey sharing session from the mapView's UI stack.
  self.mapView.hide(journeySharingSession)
}

Objective-C

/*
 * MapViewController.m
 */
- (void)cancelCurrentActiveTrip {
  // Stop the tripModel
  [self.currentTripModel unregisterSubscriber:self];

  // Remove the journey sharing session from the mapView's UI stack.
  [self.mapView hideMapViewSession:journeySharingSession];
}

تعديل مستوى تقدّم الرحلة

أثناء الرحلة، يمكنك إدارة مستوى تقدّم الرحلة على النحو التالي:

عند اكتمال الرحلة أو إلغائها، أوقِف الاستماع إلى التعديلات. للاطّلاع على مثال، يُرجى مراجعة مثال إيقاف الاستماع إلى التعديلات.

مثال بدء الاستماع إلى التعديلات

يوضّح المثال التالي كيفية تسجيل معاودة الاتصال tripModel.

Swift

/*
 * MapViewController.swift
 */
override func viewDidLoad() {
  super.viewDidLoad()
  // Register for trip update events.
  self.currentTripModel.register(self)
}

Objective-C

/*
 * MapViewController.m
 */
- (void)viewDidLoad {
  [super viewDidLoad];
  // Register for trip update events.
  [self.currentTripModel registerSubscriber:self];
  ...
}

مثال إيقاف الاستماع إلى التعديلات

يوضّح المثال التالي كيفية إلغاء تسجيل معاودة الاتصال tripModel.

Swift

/*
 * MapViewController.swift
 */
deinit {
  self.currentTripModel.unregisterSubscriber(self)
}

Objective-C

/*
 * MapViewController.m
 */
- (void)dealloc {
  [self.currentTripModel unregisterSubscriber:self];
  ...
}

مثال التعامل مع التعديلات على الرحلة

يوضّح المثال التالي كيفية تنفيذ بروتوكول GMTCTripModelSubscriber للتعامل مع معاودات الاتصال عند تعديل حالة الرحلة.

Swift

/*
 * MapViewController.swift
 */
func tripModel(_: GMTCTripModel, didUpdate trip: GMTSTrip?, updatedPropertyFields: GMTSTripPropertyFields) {
  // Update the UI with the new `trip` data.
  self.updateUI(with: trip)
}

func tripModel(_: GMTCTripModel, didUpdate tripStatus: GMTSTripStatus) {
  // Handle trip status did change.
}

func tripModel(_: GMTCTripModel, didUpdateActiveRouteRemainingDistance activeRouteRemainingDistance: Int32) {
  // Handle remaining distance of active route did update.
}

func tripModel(_: GMTCTripModel, didUpdateActiveRoute activeRoute: [GMTSLatLng]?) {
  // Handle trip active route did update.
}

func tripModel(_: GMTCTripModel, didUpdate vehicleLocation: GMTSVehicleLocation?) {
  // Handle vehicle location did update.
}

func tripModel(_: GMTCTripModel, didUpdatePickupLocation pickupLocation: GMTSTerminalLocation?) {
  // Handle pickup location did update.
}

func tripModel(_: GMTCTripModel, didUpdateDropoffLocation dropoffLocation: GMTSTerminalLocation?) {
  // Handle drop off location did update.
}

func tripModel(_: GMTCTripModel, didUpdatePickupETA pickupETA: TimeInterval) {
  // Handle the pickup ETA did update.
}

func tripModel(_: GMTCTripModel, didUpdateDropoffETA dropoffETA: TimeInterval) {
  // Handle the drop off ETA did update.
}

func tripModel(_: GMTCTripModel, didUpdateRemaining remainingWaypoints: [GMTSTripWaypoint]?) {
  // Handle updates to the pickup, dropoff or intermediate destinations of the trip.
}

func tripModel(_: GMTCTripModel, didFailUpdateTripWithError error: Error?) {
  // Handle the error.
}

func tripModel(_: GMTCTripModel, didUpdateIntermediateDestinations intermediateDestinations: [GMTSTerminalLocation]?) {
  // Handle the intermediate destinations being updated.
}

func tripModel(_: GMTCTripModel, didUpdateActiveRouteTraffic activeRouteTraffic: GMTSTrafficData?) {
  // Handle trip active route traffic being updated.
}

Objective-C

/*
 * MapViewController.m
 */
#pragma mark - GMTCTripModelSubscriber implementation

- (void)tripModel:(GMTCTripModel *)tripModel
            didUpdateTrip:(nullable GMTSTrip *)trip
    updatedPropertyFields:(enum GMTSTripPropertyFields)updatedPropertyFields {
  // Update the UI with the new `trip` data.
  [self updateUIWithTrip:trip];
  ...
}

- (void)tripModel:(GMTCTripModel *)tripModel didUpdateTripStatus:(enum GMTSTripStatus)tripStatus {
  // Handle trip status did change.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateActiveRouteRemainingDistance:(int32_t)activeRouteRemainingDistance {
   // Handle remaining distance of active route did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateActiveRoute:(nullable NSArray<GMTSLatLng *> *)activeRoute {
  // Handle trip active route did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateVehicleLocation:(nullable GMTSVehicleLocation *)vehicleLocation {
  // Handle vehicle location did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdatePickupLocation:(nullable GMTSTerminalLocation *)pickupLocation {
  // Handle pickup location did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateDropoffLocation:(nullable GMTSTerminalLocation *)dropoffLocation {
  // Handle drop off location did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel didUpdatePickupETA:(NSTimeInterval)pickupETA {
  // Handle the pickup ETA did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateRemainingWaypoints:(nullable NSArray<GMTSTripWaypoint *> *)remainingWaypoints {
  // Handle updates to the pickup, dropoff or intermediate destinations of the trip.
}

- (void)tripModel:(GMTCTripModel *)tripModel didUpdateDropoffETA:(NSTimeInterval)dropoffETA {
  // Handle the drop off ETA did update.
}

- (void)tripModel:(GMTCTripModel *)tripModel didFailUpdateTripWithError:(nullable NSError *)error {
  // Handle the error.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateIntermediateDestinations:
        (nullable NSArray<GMTSTerminalLocation *> *)intermediateDestinations {
  // Handle the intermediate destinations being updated.
}

- (void)tripModel:(GMTCTripModel *)tripModel
    didUpdateActiveRouteTraffic:(nullable GMTSTrafficData *)activeRouteTraffic {
  // Handle trip active route traffic being updated.
}

التعامل مع أخطاء الرحلة

إذا اشتركت في tripModel وحدث خطأ، يمكنك الحصول على معاودة الاتصال لـ tripModel من خلال تنفيذ طريقة المفوّض tripModel(_:didFailUpdateTripWithError:). تتّبع رسائل الخطأ معيار Google Cloud Error. للتعرّف على تعريفات رسائل الخطأ التفصيلية وجميع رموز الخطأ، يُرجى الرجوع إلى مستندات Google Cloud Errors.

في ما يلي بعض الأخطاء الشائعة التي يمكن أن تحدث أثناء رصد الرحلة:

HTTP متوسط عائد النقرة الوصف
400 INVALID_ARGUMENT حدّد العميل اسم رحلة غير صالح. يجب أن يتّبع اسم الرحلة التنسيق providers/{provider_id}/trips/{trip_id}. يجب أن يكون provider_id هو رقم تعريف مشروع Cloud الذي يملكه مقدّم الخدمة.
401 عدم الحصول على المصادقة يظهر هذا الخطأ إذا لم تكن هناك بيانات اعتماد مصادقة صالحة. على سبيل المثال، إذا تم توقيع رمز JWT المميّز بدون رقم تعريف رحلة أو إذا انتهت صلاحية رمز JWT المميّز.
403 PERMISSION_DENIED يظهر هذا الخطأ إذا لم يكن لدى العميل إذن كافٍ (على سبيل المثال، إذا حاول مستخدم لديه دور المستهلك استدعاء updateTrip)، أو إذا كان رمز JWT المميّز غير صالح، أو إذا لم تكن واجهة برمجة التطبيقات مفعّلة لمشروع العميل. قد يكون رمز JWT المميّز غير متوفّر أو تم توقيعه باستخدام رقم تعريف رحلة لا يتطابق مع رقم تعريف الرحلة المطلوب.
429 RESOURCE_EXHAUSTED حصة الموارد تساوي صفرًا أو يتجاوز معدّل حركة البيانات الحدّ الأقصى المسموح به.
503 UNAVAILABLE الخدمة غير متاحة. عادةً ما يكون الخادم معطّلاً.
504 DEADLINE_EXCEEDED انتهت المهلة النهائية لتقديم الطلب. لا يظهر هذا الخطأ إلا إذا حدّد المتصل مهلة أق101} صر من المهلة التلقائية للطريقة (أي أنّ المهلة المطلوبة غير كافية للخادم لمعالجة الطلب) ولم يكتمل الطلب خلال المهلة.

التعامل مع أخطاء حزمة تطوير برامج المستهلك (SDK)

تُرسِل حزمة تطوير برامج المستهلك (SDK) أخطاء تعديل الرحلة إلى تطبيق المستهلك باستخدام آلية معاودة الاتصال. إنّ مَعلمة معاودة الاتصال هي نوع القيمة التي تم إرجاعها خاص بالنظام الأساسي ( TripUpdateError على Android و NSError على iOS).

استخراج رموز الحالة

عادةً ما تكون الأخطاء التي يتم تمريرها إلى معاودة الاتصال هي أخطاء gRPC، ويمكنك أيضًا استخراج معلومات إضافية منها في شكل رمز حالة. للاطّلاع على القائمة الكاملة برموز الحالة، يُرجى مراجعة رموز الحالة واستخدامها في gRPC.

Swift

يتم استدعاء NSError في tripModel(_:didFailUpdateTripWithError:).

// Called when there is a trip update error.
func tripModel(_ tripModel: GMTCTripModel, didFailUpdateTripWithError error: Error?) {
  // Check to see if the error comes from gRPC.
  if let error = error as NSError?, error.domain == "io.grpc" {
    let gRPCErrorCode = error.code
    ...
  }
}

Objective-C

يتم استدعاء NSError في tripModel:didFailUpdateTripWithError:.

// Called when there is a trip update error.
- (void)tripModel:(GMTCTripModel *)tripModel didFailUpdateTripWithError:(NSError *)error {
  // Check to see if the error comes from gRPC.
  if ([error.domain isEqualToString:@"io.grpc"]) {
    NSInteger gRPCErrorCode = error.code;
    ...
  }
}

تفسير رموز الحالة

تغطّي رموز الحالة نوعَين من الأخطاء: الأخطاء المتعلّقة بالخادم والشبكة، والأخطاء من جهة العميل.

أخطاء الخادم والشبكة

تخصّ رموز الحالة التالية أخطاء الشبكة أو الخادم، ولا عليك اتّخاذ أي إجراء لحلّها. تستردّ حزمة تطوير برامج المستهلك (SDK) تلقائيًا من هذه الأخطاء.

رمز الحالةالوصف
ABORTED توقّف الخادم عن إرسال الردّ. عادةً ما يكون ذلك بسبب مشكلة في الخادم.
تم إلغاؤها أنهى الخادم الردّ الصادر. يحدث ذلك عادةً عندما يتم إرسال التطبيق إلى الخلفية، أو عند حدوث تغيير في الحالة في
تطبيق المستهلك.
INTERRUPTED
DEADLINE_EXCEEDED استغرق الخادم وقتًا طويلاً للاستجابة.
UNAVAILABLE الخادم غير متوفّر. عادةً ما يكون ذلك بسبب مشكلة في الشبكة

أخطاء العميل

تخصّ رموز الحالة التالية أخطاء العميل، وعليك اتّخاذ إجراء لحلّها. تستمر حزمة تطوير برامج المستهلك (SDK) في إعادة محاولة تحديث الرحلة إلى أن توقف مشاركة الرحلة، ولكن لن يتم استردادها إلى أن تتّخذ إجراءً.

رمز الحالةالوصف
INVALID_ARGUMENT حدّد تطبيق المستهلك اسم رحلة غير صالح. يجب أن يتّبع اسم الرحلة التنسيق providers/{provider_id}/trips/{trip_id}.
NOT_FOUND لم يتم إنشاء الرحلة مطلقًا.
PERMISSION_DENIED لا يملك تطبيق المستهلك أذونات كافية. يحدث هذا الخطأ في الحالات التالية:
  • لا يملك تطبيق المستهلك أذونات
  • حزمة تطوير برامج المستهلك (SDK) غير مفعّلة للمشروع في Google Cloud Console.
  • رمز JWT المميّز غير متوفّر أو غير صالح.
  • تم توقيع رمز JWT المميّز باستخدام رقم تعريف رحلة لا يتطابق مع الـ رحلة المطلوبة.
RESOURCE_EXHAUSTED حصة الموارد تساوي صفرًا أو يتجاوز معدّل حركة البيانات الحدّ الأقصى المسموح به.
عدم الحصول على المصادقة تعذّرت مصادقة الطلب بسبب رمز JWT المميّز غير الصالح. يحدث هذا الخطأ إما عند توقيع رمز JWT المميّز بدون رقم تعريف رحلة، أو عند انتهاء صلاحية رمز JWT المميّز.