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

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

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

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

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

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

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

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