使用 Driver SDK 前,您必須先按照下列步驟初始化 Navigation SDK 和 Driver SDK:
從
NavigationApi
取得Navigator
物件。Java
NavigationApi.getNavigator( this, // Activity new NavigationApi.NavigatorListener() { @Override public void onNavigatorReady(Navigator navigator) { // Keep a reference to the Navigator (used to configure and start nav) this.navigator = navigator; } } );
Kotlin
NavigationApi.getNavigator( this, // Activity object : NavigatorListener() { override fun onNavigatorReady(navigator: Navigator) { // Keep a reference to the Navigator (used to configure and start nav) this@myActivity.navigator = navigator } }, )
建立
DriverContext
物件,填入必要欄位。目的地: 初始化DriverContext
物件,您必須輸入專案 ID 也就是 Google Cloud 專案的providerId
資訊 有關設定 Google Cloud 專案的指示,請參閱 建立 Fleet Engine 專案。Java
DriverContext driverContext = DriverContext.builder(application) .setProviderId(providerId) .setVehicleId(vehicleId) .setAuthTokenFactory(authTokenFactory) .setNavigator(navigator) .setRoadSnappedLocationProvider( NavigationApi.getRoadSnappedLocationProvider(application)) .build();
Kotlin
val driverContext = DriverContext.builder(application) .setProviderId(providerId) .setVehicleId(vehicleId) .setAuthTokenFactory(authTokenFactory) .setNavigator(navigator) .setRoadSnappedLocationProvider(NavigationApi.getRoadSnappedLocationProvider(application)) .build()
使用
DriverContext
物件初始化*DriverApi
。Java
RidesharingDriverApi ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext);
Kotlin
val ridesharingDriverApi = RidesharingDriverApi.createInstance(driverContext)
從 API 物件取得
RidesharingVehicleReporter
。(*VehicleReporter
擴充NavigationVehicleReporter
)。Java
RidesharingVehicleReporter vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter();
Kotlin
val vehicleReporter = ridesharingDriverApi.getRidesharingVehicleReporter()
SSL/TLS 相關注意事項
在內部,驅動程式 SDK 實作會使用
可透過 SSL/TLS 安全地與 Fleet Engine 服務進行通訊。Android API 23 以下版本可能需要 SecurityProvider
修補程式才能與伺服器通訊。如要進一步瞭解如何在 Android 中使用 SSL,請參閱「安全性 GMS 供應器」一文。本文也包含修補安全性提供者的程式碼範例。