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