本節說明如何準備車輛,以便接送乘客。您必須完成下列每個步驟,後端才能將車輛與行程配對。
設定事件監聽器
初始化 Driver SDK 並建立 GMTDRidesharingDriverAPI
例項後,您可以設定事件監聽器,監控傳送至 Fleet Engine 和後端的車輛更新是否成功。這些事件監聽器可在駕駛應用程式中觸發動作,例如在與後端的通訊失敗時通知駕駛人。
監聽車輛更新事件
當駕駛者在駕駛者應用程式中啟用位置更新功能時,Driver SDK 會透過 GMTDVehicleReporter
類別,將定期的車輛更新資料傳送至車隊引擎和客戶後端。您可以設定 GMTDVehicleReporterListener
通訊協定,讓應用程式回應更新事件。
您可以使用 GMTDVehicleReporterListener
處理下列事件:
vehicleReporter:didSucceedVehicleUpdate
通知 Driver 應用程式,後端服務已成功收到車輛位置和狀態更新。
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
類別,為 Driver 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 和客戶後端服務比對並將車輛指派給行程後,當 GMSNavigator
處於導航模式 (也就是透過 setDestinations
設定目的地時),GMTDVehicleReporter
類別會自動傳送路線更新。
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 會以 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