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