本節說明如何為車輛做好行程準備。您必須完成 每一個步驟,才能在後端比對車輛與行程之前。
設定事件監聽器
初始化驅動程式 SDK 並建立 GMTDRidesharingDriverAPI
後
您可以設定事件監聽器,藉此監控
車輛更新資料會傳送至 Fleet Engine 和您的後端這些事件監聽器可以
在駕駛應用程式中觸發動作,例如:在發生以下情況時通知駕駛:
與後端通訊失敗
監聽車輛更新事件
當驅動程式在驅動程式應用程式中啟用位置更新時,驅動程式 SDK
定期將車輛更新資訊傳送給 Fleet Engine,
透過 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 透過
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
GMTDVehicleReporter
。然後是 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