本節說明如何準備車輛,以便接送乘客。您必須完成下列每個步驟,後端才能將車輛與行程配對。
設定事件監聽器
初始化 Driver SDK 並建立 GMTDRidesharingDriverAPI
例項後,您可以設定事件監聽器,監控傳送至 Fleet Engine 和後端的車輛更新是否成功。這些事件監聽器可在駕駛應用程式中觸發動作,例如在與後端的通訊失敗時通知駕駛人。
監聽車輛更新事件
當駕駛人在驅動程式應用程式中啟用位置更新功能後,驅動程式 SDK 會透過 GMTDVehicleReporter
類別,將一般車輛更新傳送至 Fleet Engine 和客戶後端。您可以設定 GMTDVehicleReporterListener
通訊協定,讓應用程式回應更新事件。
您可以使用 GMTDVehicleReporterListener
處理下列事件:
vehicleReporter:didSucceedVehicleUpdate
通知駕駛應用程式,後端服務已成功收到車輛位置和狀態更新。
vehicleReporter:didFailVehicleUpdate:withError
通知事件監聽器車輛更新失敗。只要駕駛人啟用位置更新功能,
GMTDVehicleReporter
類別就會持續將最新資料傳送至車隊引擎。
以下範例說明如何設定 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 透過 GMSRoadSnappedLocationProvider
類別為驅動程式 SDK 提供位置更新通知。如要接收這些更新,您必須將 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
啟用位置更新通知
如要啟用位置更新功能,請在驅動程式應用程式中,將 GMTDVehicleReporter
上的 locationTrackingEnabled
設為 true
。這樣一來,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
時,驅動程式 SDK 會以 10 秒的間隔,將行程和車輛更新資料傳送至 Fleet Engine。您可以使用 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