在使用 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
对象,您必须输入 Google Cloud 项目的项目 ID 作为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
。DeliveryDriverApi driverApi = DeliveryDriverApi.createInstance(driverContext);
从 API 对象获取
DeliveryVehicleReporter
。(DeliveryVehicleReporter
扩展了NavigationVehicleReporter
。)DeliveryVehicleReporter vehicleReporter = driverApi.getDeliveryVehicleReporter();
SSL/TLS 备注
在内部,Driver SDK 实现使用 SSL/TLS 与车队引擎服务进行安全通信。Android API 23 或更低版本可能需要 SecurityProvider
补丁才能与服务器通信。如需详细了解如何在 Android 中使用 SSL,请参阅 Security GMS Provider。本文还包含用于修补安全提供程序的代码示例。