מכינים את הרכב

בקטע הזה מוסבר איך להכין את הרכב לנסיעות. כדי שה-backend יוכל להתאים בין רכב לנסיעה, צריך לבצע את כל השלבים הבאים.

הגדרת מאזינים

אחרי שמפעילים את Driver SDK ויוצרים מכונה של GMTDRidesharingDriverAPI, אפשר להגדיר רכיבי event listener כדי לעקוב אחרי ההצלחה או הכישלון של עדכוני הרכב שנשלחים ל-Fleet Engine ול-backend. המאזינים האלה יכולים להפעיל פעולות באפליקציה של הנהג, למשל להודיע לנהג אם התקשורת עם הקצה העורפי נכשלה.

האזנה לאירועים של עדכוני רכב

כשהנהג מפעיל עדכונים לגבי מיקום באפליקציית הנהג, ה-Driver SDK שולח עדכונים שוטפים על הרכב ל-Fleet Engine ול-backend של הלקוח דרך המחלקה GMTDVehicleReporter. כדי שהאפליקציה תגיב לאירועי עדכון, צריך להגדיר את הפרוטוקול GMTDVehicleReporterListener.

בעזרת GMTDVehicleReporterListener, אתם יכולים לטפל באירועים הבאים:

  • vehicleReporter:didSucceedVehicleUpdate

    מודיעה לאפליקציית הנהג ששירותי הקצה העורפי קיבלו את עדכון המיקום והמצב של הרכב.

  • vehicleReporter:didFailVehicleUpdate:withError

    הודעה למאזין שעדכון הרכב נכשל. כל עוד הנהג הפעיל את עדכוני המיקום, המחלקה GMTDVehicleReporter ממשיכה לשלוח את הנתונים העדכניים ביותר אל Fleet Engine.

בדוגמאות הבאות מוסבר איך להגדיר את GMTDVehicleReporterListener לטיפול באירועים האלה:

Swift

import GoogleRidesharingDriver

private let providerID = "INSERT_YOUR_PROVIDER_ID"

class SampleViewController: UIViewController, GMTDVehicleReporterListener {
  private let mapView: GMSMapView

  override func viewDidLoad() {
    // Assumes you have implemented the sample code up to this step.
    ridesharingDriverAPI.vehicleReporter.add(self)
  }

  func vehicleReporter(_ vehicleReporter: GMTDVehicleReporter, didSucceed vehicleUpdate: GMTDVehicleUpdate) {
    // Handle update succeeded.
  }

  func vehicleReporter(_ vehicleReporter: GMTDVehicleReporter, didFail vehicleUpdate: GMTDVehicleUpdate, withError error: Error) {
    // Handle update failed.
  }
}

Objective-C

/**
 *   SampleViewController.h
 */
@interface SampleViewController : UIViewController<GMTDVehicleReporterListener>
@end

/**
 *   SampleViewController.m
 */
#import "SampleViewController.h"
#import "SampleAccessTokenProvider.h"
#import <GoogleRidesharingDriver/GoogleRidesharingDriver.h>

static NSString *const PROVIDER_ID = @"INSERT_YOUR_PROVIDER_ID";

@implementation SampleViewController {
  GMSMapView *_mapView;
}

- (void)viewDidLoad {
  // Assumes you have implemented the sample code up to this step.
  [ridesharingDriverAPI.vehicleReporter addListener:self];
}

- (void)vehicleReporter:(GMTDVehicleReporter *)vehicleReporter didSucceedVehicleUpdate:(GMTDVehicleUpdate *)vehicleUpdate {
  // Handle update succeeded.
}

- (void)vehicleReporter:(GMTDVehicleReporter *)vehicleReporter didFailVehicleUpdate:(GMTDVehicleUpdate *)vehicleUpdate withError:(NSError *)error {
  // Handle update failed.
}

@end

האזנה לעדכונים לגבי מיקום הרכב

‫Navigation SDK מספק עדכוני מיקום ל-Driver SDK דרך הכיתה GMSRoadSnappedLocationProvider. כדי לקבל את העדכונים האלה, צריך להגדיר את GMTDVehicleReporter כמאזין.

Swift

import GoogleRidesharingDriver

private let providerID = "INSERT_YOUR_PROVIDER_ID"

class SampleViewController: UIViewController, GMTDVehicleReporterListener {
  private let mapView: GMSMapView

  override func viewDidLoad() {
    // Assumes you have implemented the sample code up to this step.
    if let roadSnappedLocationProvider = mapView.roadSnappedLocationProvider {
      roadSnappedLocationProvider.add(ridesharingDriverAPI.vehicleReporter)
      roadSnappedLocationProvider.startUpdatingLocation()
    }
  }
}

Objective-C

/**
 *   SampleViewController.h
 */
@interface SampleViewController : UIViewController<GMTDVehicleReporterListener>
@end

/**
 *   SampleViewController.m
 */
#import "SampleViewController.h"
#import "SampleAccessTokenProvider.h"
#import <GoogleRidesharingDriver/GoogleRidesharingDriver.h>

static NSString *const PROVIDER_ID = @"INSERT_YOUR_PROVIDER_ID";

@implementation SampleViewController {
  GMSMapView *_mapView;
}

- (void)viewDidLoad {
  // Assumes you have implemented the sample code up to this step.
  [_mapView.roadSnappedLocationProvider addListener:ridesharingDriverAPI.vehicleReporter];
  [_mapView.roadSnappedLocationProvider startUpdatingLocation];
}

@end

הפעלת עדכונים לגבי מיקום

כדי להפעיל עדכוני מיקום, מגדירים את locationTrackingEnabled ל-true ב-GMTDVehicleReporter באפליקציה לנהגים. לאחר מכן, המחלקה GMTDVehicleReporter שולחת באופן אוטומטי עדכוני מיקום ל-Fleet Engine. אחרי ששירותי הקצה העורפי של Fleet Engine ושל הלקוח מתאימים את הרכב לנסיעה ומקצים אותו אליה, המחלקה GMTDVehicleReporter שולחת עדכוני מסלול באופן אוטומטי כש-GMSNavigator נמצא במצב ניווט, כלומר כשמוגדר יעד באמצעות setDestinations.

ה-Driver SDK מגדיר את המסלול כך שיתאים למסלול הניווט הנוכחי של הנהג. כדי לוודא עדכון לגבי מיקום מדויק, צריך להגדיר את ציון הדרך ב-setDestinations כך שיתאים ליעד ב-Fleet Engine.

בדוגמה הבאה אפשר לראות איך מפעילים עדכונים לגבי מיקום:

Swift

import GoogleRidesharingDriver

private let providerID = "INSERT_YOUR_PROVIDER_ID"

class SampleViewController: UIViewController, GMTDVehicleReporterListener {
  private let mapView: GMSMapView

  override func viewDidLoad() {
    // Assumes you have implemented the sample code up to this step.
    ridesharingDriverAPI.vehicleReporter.locationTrackingEnabled = true
  }
}

Objective-C

/**
 *   SampleViewController.m
 */
#import "SampleViewController.h"
#import "SampleAccessTokenProvider.h"
#import <GoogleRidesharingDriver/GoogleRidesharingDriver.h>

static NSString *const PROVIDER_ID = @"INSERT_YOUR_PROVIDER_ID";

@implementation SampleViewController {
  GMSMapView *_mapView;
}

- (void)viewDidLoad {
  // Assumes you have implemented the sample code up to this step.
  ridesharingDriverAPI.vehicleReporter.locationTrackingEnabled = YES;
}

@end

הגדרת מרווח הזמן לעדכון

כברירת מחדל, כשמגדירים את locationTrackingEnabled ל-true, ה-Driver SDK שולח עדכונים על הנסיעה ועל הרכב ל-Fleet Engine במרווחים של 10 שניות. אפשר לשנות את מרווח הזמן בין העדכונים באמצעות locationUpdateInterval, למרווח זמן מינימלי של 5 שניות או למרווח זמן מקסימלי של 60 שניות. עדכונים תכופים יותר עלולים לגרום לבקשות איטיות יותר ולשגיאות.

הגדרת מצב הרכב ל"אונליין"

אחרי שמפעילים את עדכוני המיקום, מגדירים את מצב הרכב ל-ONLINE כדי שהרכב יהיה זמין לשאילתות חיפוש ב-Fleet Engine.

בדוגמאות הבאות מוסבר איך להגדיר את מצב הרכב ל-ONLINE. מידע נוסף זמין במאמר updateVehicleState.

Swift

import GoogleRidesharingDriver

private let providerID = "INSERT_YOUR_PROVIDER_ID"

class SampleViewController: UIViewController, GMTDVehicleReporterListener {
  private let mapView: GMSMapView

  override func viewDidLoad() {
    // Assumes you have implemented the sample code up to this step.
    ridesharingDriverAPI.vehicleReporter.update(.online)
  }
}

Objective-C

#import "SampleViewController.h"
#import "SampleAccessTokenProvider.h"
#import <GoogleRidesharingDriver/GoogleRidesharingDriver.h>

static NSString *const PROVIDER_ID = @"INSERT_YOUR_PROVIDER_ID";

@implementation SampleViewController {
  GMSMapView *_mapView;
}

- (void)viewDidLoad {
  // Assumes you have implemented the sample code up to this step.
  [ridesharingDriverAPI.vehicleReporter
                                   updateVehicleState:GMTDVehicleStateOnline];
}

@end

המאמרים הבאים

הגדרת פרטי הנסיעה