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

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

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

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

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

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

באמצעות GMTDVehicleReporterListener, אפשר לנהל את האירועים הבאים:

  • vehicleReporter:didSucceedVehicleUpdate

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

  • 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 on GMTDVehicleReporter באפליקציית הנהג. לאחר מכן, המחלקה GMTDVehicleReporter שולחת עדכוני מיקום ל-Fleet Engine באופן אוטומטי. אחרי ששירותי ה-Backend של 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

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

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