開始使用 Android 版 Driver SDK

基本系統需求

行動裝置必須搭載 Android 6.0。 (API 級別 23) 以上版本。

建構和依附元件設定

您可以從 Google Maven 存放區取得 4.99 以上版本的驅動程式 SDK。

Gradle

請將以下內容新增到 build.gradle 檔案中:

repositories {
    ...
    google()
}

Maven

請將以下內容新增到 pom.xml 檔案中:

<project>
  ...
  <repositories>
    <repository>
      <id>google-maven-repository</id>
      <url>https://maven.google.com</url>
    </repository>
  </repositories>
  ...
</project>

專案設定

如要使用 Driver SDK,應用程式必須指定 minSdkVersion 23 以上版本。如要 資訊,請參閱 版本資訊

如要執行使用 Driver SDK 建構的應用程式,Android 裝置必須具備 Google Play 服務 已安裝。

設定開發專案

設定開發專案並取得 API 金鑰 建立專案:

  1. 建立新的 Google Cloud 控制台 或選取現有專案 驅動程式 SDK 版本請稍候片刻,直到 新專案會顯示在 Google Cloud 控制台中

  2. 如要執行試用版應用程式,專案必須具備 Maps SDK 的存取權 。在 Google Cloud 控制台中,選取 API 與服務 >程式庫,然後搜尋並啟用 Maps SDK for Android。

  3. 選取專案的 API 金鑰,取得專案的 API 金鑰 API 與服務 >憑證 >建立憑證 >API 金鑰。 如要進一步瞭解如何取得 API 金鑰,請參閱 取得 API 金鑰

在應用程式中新增驅動程式 SDK

您可以從 Google Maven 存放區取得驅動程式 SDK。 存放區包括 SDK 的專案物件模型 (.pom) 檔案和 Javadocs。 在應用程式中新增 Driver SDK:

  1. 將以下依附元件新增至 Gradle 或 Maven 設定。 將所選版本的 VERSION_NUMBER 預留位置替換成 驅動程式。

    Gradle

    請將以下內容新增到您的 build.gradle 中:

    dependencies {
      ...
      implementation 'com.google.android.libraries.mapsplatform.transportation:transportation-driver:VERSION_NUMBER'
    }
    

    Maven

    請將以下內容新增到您的 pom.xml 中:

    <dependencies>
      ...
      <dependency>
        <groupId>com.google.android.libraries.mapsplatform.transportation</groupId>
        <artifactId>transportation-driver</artifactId>
        <version>VERSION_NUMBER</version>
      </dependency>
    </dependencies>
    
  2. 驅動程式 SDK 依附於 Navigation SDK,此依附元件是由 以便在需要特定版本的 Navigation SDK 時 明確定義,如下所示: 省略上述程式碼區塊後,專案就會一律下載 主要發布版本中最新版 Navigation SDK。 請注意,最新版驅動程式 SDK 和 Navigation SDK 發布前已進行嚴格的測試。

    排列開發和發布版本的依附元件設定 部署應用程式

    Gradle

    請將以下內容新增到您的 build.gradle 中:

    dependencies {
      ...
      implementation 'com.google.android.libraries.navigation:navigation:5.0.0'
    }
    

    Maven

    請將以下內容新增到您的 pom.xml 中:

    <dependencies>
      ...
      <dependency>
        <groupId>com.google.android.libraries.navigation</groupId>
        <artifactId>navigation</artifactId>
        <version>5.0.0</version>
      </dependency>
    </dependencies>
    

更新應用程式的資訊清單

在應用程式中加入 Driver SDK 後,即可 更新應用程式的 AndroidManifest.xml 檔案,更新應用程式的資訊清單。

<application> 元素中加入 API 金鑰。您必須使用 您在 Google Cloud 專案中 設定開發專案

例如,將 PASTE_YOUR_API_KEY_HERE 替換成您的 API 金鑰 均列於應用程式的中繼資料中:

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="PASTE_YOUR_API_KEY_HERE"/>

以下範例顯示範例應用程式的完整資訊清單:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.driverapidemo" >
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/_AppTheme" >

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="PASTE_YOUR_API_KEY_HERE"/>

        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

在應用程式中加入必要的作者資訊

如果您在應用程式中使用 Driver SDK,則必須納入 將作者資訊文字和開放原始碼授權納入應用程式的法律聲明中 專區。最好的方法是將出處資訊列為獨立的選單項目,或者 「關於」選單項目的一部分

您可以在「third_party_licenses.txt」中找到授權資訊該檔案位於 已取消封存的 AAR 檔案

請參閱 https://developers.google.com/android/guides/opensource ,瞭解如何納入開放原始碼通知。

依附元件

驅動程式 SDK 會使用 gRPC 至 與 Fleet Engine 伺服器通訊如果您尚未加入 gRPC, 您可能需要宣告下列依附元件:

dependencies {
    implementation 'io.grpc:grpc-android:1.12.0'
    implementation 'io.grpc:grpc-okhttp:1.12.0'
}

如果沒有這些依附元件,驅動程式 SDK 就會 執行階段發生錯誤 嘗試與 Fleet Engine 伺服器通訊

如果您使用 ProGuard 您可能需要將下列幾行程式碼加入 ProGuard 中,將建構作業最佳化 設定檔:

-dontwarn com.google.**
-dontwarn io.grpc.**
-dontwarn okio.**

支援的最低 API 級別為 23。

初始化 SDK

您必須提供供應商 ID (通常為 Google Cloud 專案 ID),才能 初始化 FleetEngine 物件。進一步瞭解如何設定 Google Cloud Cloud 專案,請參閱 驗證與授權

使用 Driver SDK 前,必須先初始化 Navigation SDK。如要初始化 SDK:

  1. NavigationApi 取得 Navigator 物件。

    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;
            }
          }
    );
    
  2. 建立 DriverContext 物件,並填入必填欄位。

    DriverContext driverContext = DriverContext.builder(application)
                 .setProviderId(providerId)
                 .setVehicleId(vehicleId)
                 .setAuthTokenFactory(authTokenFactory)
                 .setNavigator(navigator)
                 .setRoadSnappedLocationProvider(
                     NavigationApi.getRoadSnappedLocationProvider(application))
                 .build()
    
  3. 使用 DriverContext 物件初始化 *DriverApi

    DeliveryDriverApi driverApi = DeliveryDriverApi.createInstance(driverContext);
    
  4. 從 API 物件取得 DeliveryVehicleReporter。 (DeliveryVehicleReporter 擴充 NavigationVehicleReporter)。

    DeliveryVehicleReporter vehicleReporter = driverApi.getDeliveryVehicleReporter();
    

透過 AuthTokenFactory 進行驗證

當驅動程式 SDK 產生位置更新時, 必須將這些更新傳送至 完成遷移程序為了驗證這些要求, 驅動程式 SDK 呼叫呼叫端提供的 AuthTokenFactory 的執行個體。 工廠負責產生所在位置的驗證權杖 更新時間。

每個開發人員的情況產生權杖的確切方式。 不過,實作可能需要:

  • 從 HTTPS 伺服器擷取驗證權杖 (可能採用 JSON 格式)。
  • 剖析及快取權杖。
  • 請在權杖過期時重新整理。

如要進一步瞭解 Fleet Engine 伺服器預期的權杖,請參閱 建立用於授權的 JSON Web Token (JWT)

以下是 AuthTokenFactory 的架構實作:

class JsonAuthTokenFactory implements AuthTokenFactory {
  private String vehicleServiceToken;  // initially null
  private long expiryTimeMs = 0;
  private String vehicleId;

  // This method is called on a thread whose only responsibility is to send
  // location updates. Blocking is OK, but just know that no location updates
  // can occur until this method returns.
  @Override
  public String getToken(AuthTokenContext authTokenContext) {
    String vehicleId = requireNonNull(context.getVehicleId());

    if (System.currentTimeMillis() > expiryTimeMs || !vehicleId.equals(this.vehicleId)) {
      // The token has expired, go get a new one.
      fetchNewToken(vehicleId);
    }

    return vehicleServiceToken;
  }

  private void fetchNewToken(String vehicleId) {
    String url = "https://yourauthserver.example/token/" + vehicleId;

    try (Reader r = new InputStreamReader(new URL(url).openStream())) {
      com.google.gson.JsonObject obj
          = com.google.gson.JsonParser.parseReader(r).getAsJsonObject();
      vehicleServiceToken = obj.get("VehicleServiceToken").getAsString();
      expiryTimeMs = obj.get("TokenExpiryMs").getAsLong();

      // The expiry time could be an hour from now, but just to try and avoid
      // passing expired tokens, we subtract 10 minutes from that time.
      expiryTimeMs -= 10 * 60 * 1000;
      this.vehicleId = vehicleId;
    } catch (IOException e) {
      // It's OK to throw exceptions here. The StatusListener you passed to
      // create the DriverContext class will be notified and passed along the failed
      // update warning.
      throw new RuntimeException("Could not get auth token", e);
    }
  }
}

這個特定實作會使用內建的 Java HTTP 用戶端,擷取 產生 JSON 格式的權杖權杖是 儲存以供重複使用如果舊的權杖在 10 分鐘內,系統會重新擷取權杖 才會達到使用門檻

實作項目可能會有不同,例如使用背景執行緒 重新整理符記

除非發生 AuthTokenFactory 例外狀況,否則系統會將例外狀況視為暫時處理 重複執行。嘗試多次嘗試後,驅動程式 SDK 系統會假設這是永久性錯誤,並停止嘗試傳送更新。

StatusListener」的狀態和 Error Reporting

由於驅動程式 SDK 會在 背景,使用 StatusListener,以便在特定情況下觸發通知 事件,例如錯誤、警告或偵錯訊息錯誤可能是 短暫出現 (例如 BACKEND_CONNECTIVITY_ERROR),否則可能會 這會永久停止位置更新 (例如 VEHICLE_NOT_FOUND、 表示設定錯誤)。

您可以自行提供選用的 StatusListener 實作項目,如下所示:

class MyStatusListener implements StatusListener {
  /** Called when background status is updated, during actions such as location reporting. */
  @Override
  public void updateStatus(
    StatusLevel statusLevel,
    StatusCode statusCode,
    String statusMsg) {
    // Status handling stuff goes here.
    // StatusLevel may be DEBUG, INFO, WARNING, or ERROR.
    // StatusCode may be DEFAULT, UNKNOWN_ERROR, VEHICLE_NOT_FOUND,
    // BACKEND_CONNECTIVITY_ERROR, or PERMISSION_DENIED.
  }
}

SSL/TLS 注意事項

在內部,驅動程式 SDK 實作會使用 採用 SSL/TLS 進行安全通訊 與 Fleet Engine 伺服器連線舊版 Android (API 版本) 23 或更早推出的機型) 可能需要 SecurityProvider 修補程式,用於與伺服器通訊。如要進一步瞭解 有關在 Android 中使用 SSL 的資訊,請參閱 更新安全性提供者以防範安全資料傳輸層 (SSL) 漏洞。 本文也包含修補安全性提供者的程式碼範例。

啟用位置更新通知

建立 *VehicleReporter 執行個體後,啟用位置更新功能 直截了當:

DeliveryVehicleReporter reporter = ...;

reporter.enableLocationTracking();

在可能的情況下,系統會定期傳送位置更新資訊。每個地點 更新也會指出車輛已連上網路。

報表間隔預設為 10 秒,但您可以 報表間隔 FleetEngine.setLocationReportingInterval(long, TimeUnit)。最低 支援的更新間隔為 5 秒。提高更新頻率可能會導致 也能降低要求和錯誤率

停用位置更新通知

司機的班機結束後,你可以撥打電話停止更新位置 DeliveryVehicleReporter.disableLocationTracking

受信任的模型用途

本節說明如何使用 Driver SDK 實作常見用途 請謹慎選擇信任模型

建立車輛

您可以透過驅動程式 SDK 建立車輛。

建立車輛前,請務必確認 初始化 Delivery Driver API。 車輛 ID 必須使用你使用的車輛和供應商 ID 建立 期間。接著依照畫面上的說明建立車輛 在以下範例中:

DeliveryDriverApi api = DeliveryDriverApi.getInstance();
DeliveryVehicleManager vehicleManager = api.getDeliveryVehicleManager();
try {
  DeliveryVehicle vehicle = vehicleManager.createVehicle().get();
  // Handle CreateVehicleRequest DeliveryVehicle response.
} catch (Exception e) {
  // Handle CreateVehicleRequest error.
}

建立運送自取工作

您可以透過 Driver SDK 建立運送取貨工作。

建立工作前,請務必 初始化 Delivery Driver API。 必須使用驅動程式 SDK 中指定的提供者 ID 建立工作 和初始化。接著建立運送取貨工作,如下所示 範例。如要瞭解工作 ID,請參閱「工作 ID 範例」。

static final String TASK_ID = "task-8241890"; // Avoid auto-incrementing IDs.

DeliveryDriverApi api = DeliveryDriverApi.getInstance();
DeliveryTaskManager taskManager = api.getDeliveryTaskManager();
CreateDeliveryTaskRequest request = CreateDeliveryTaskRequest.builder(TASK_ID)
   .setPlannedWaypoint(Waypoint.builder().setLatLng(-6.195139, 106.820826).build())
   .setTaskDurationSeconds(2 * 60)
   .setParentId("my-tracking-id")
   .setTaskType(TaskType.DELIVERY_PICKUP)
   .build();

try {
   DeliveryTask task = taskManager.createTask(request).get();
   // Handle CreateTaskRequest DeliveryTask response.
} catch (Exception e)  {
   // Handle CreateTaskRequest error.
}

建立運送運送工作

您可以透過 Driver SDK 建立運送運送工作,

建立工作前,請務必 初始化 Delivery Driver API。 接著建立運送運送工作,如以下範例所示。 如要進一步瞭解工作 ID,請參閱 工作 ID 範例

static final String TASK_ID = "task-8241890"; // Avoid auto-incrementing IDs.

DeliveryDriverApi api = DeliveryDriverApi.getInstance();
DeliveryTaskManager taskManager = api.getDeliveryTaskManager();
CreateDeliveryTaskRequest request = CreateDeliveryTaskRequest.builder(TASK_ID)
   .setPlannedWaypoint(Waypoint.builder().setLatLng(-6.195139, 106.820826).build())
   .setTaskDurationSeconds(2 * 60)
   .setParentId("my-tracking-id")
   .setTaskType(TaskType.DELIVERY_DELIVERY)
   .build();
try {
   DeliveryTask task = taskManager.createTask(request).get();
   // Handle CreateTaskRequest DeliveryTask response.
} catch (Exception e)  {
   // Handle CreateTaskRequest error.
}

已排定時間無法使用

您可以建立指出無法使用的工作 (例如針對駕駛人) 例如車輛故障或難民事故已排定時間無法預約 任務不得包含追蹤 ID。您可以選擇提供位置。

建立工作前,請務必 初始化 Delivery Driver API。 接著建立無法使用的工作,如以下範例所示。 如要進一步瞭解工作 ID,請參閱 工作 ID 範例

static final String TASK_ID = "task-8241890"; // Avoid auto-incrementing IDs.

DeliveryDriverApi api = DeliveryDriverApi.getInstance();
DeliveryTaskManager taskManager = api.getDeliveryTaskManager();
CreateDeliveryTaskRequest request = CreateDeliveryTaskRequest.builder(TASK_ID)
   .setTaskDurationSeconds(2 * 60) // Duration or location (or both) must be provided for a BREAK task.
   .setTaskType(TaskType.UNAVAILABLE)
   .build();
try {
   DeliveryTask task = taskManager.createTask(request).get();
   // Handle CreateTaskRequest DeliveryTask response.
} catch (Exception e)  {
   // Handle CreateTaskRequest error.
}

已排定的停靠站

您可以透過 Driver SDK 建立排定的停止工作。已排定 停止工作不得包含追蹤 ID。

建立工作前,請務必 初始化 Delivery Driver API。 接著建立排定的停止工作,如以下範例所示。 如要進一步瞭解工作 ID,請參閱 工作 ID 範例

static final String TASK_ID = "task-8241890"; //  Avoid auto-incrementing IDs.

DeliveryDriverApi api = DeliveryDriverApi.getInstance();
DeliveryTaskManager taskManager = api.getDeliveryTaskManager();
CreateDeliveryTaskRequest request = CreateDeliveryTaskRequest.builder(TASK_ID)
   .setPlannedWaypoint(Waypoint.builder().setLatLng(-6.195139, 106.820826).build())
   .setTaskDurationSeconds(2 * 60)
   .setTaskType(TaskType.DELIVERY_SCHEDULED_STOP)
   .build();
try {
   DeliveryTask task = taskManager.createTask(request).get();
   // Handle CreateTaskRequest DeliveryTask response.
} catch (Exception e)  {
   // Handle CreateTaskRequest error.
}

更新工作排序

您可以前往以下位置更新指派給車輛的工作執行順序: 驅動程式 SDK。

更新工作排序後,如果有未的工作,也會指派給車輛 同時關閉先前設定的工作 系統指派的車輛專用 ID,但這些資料並未更新。指派 的工作轉移到其他車輛 (如果先前已指派給其他車輛) 車輛產生錯誤。將工作指派給新車輛前,請先關閉 然後使用新工作建立新工作

您隨時都可更新工作排序。

更新車輛的工作順序之前,請確定該車輛, 所有工作都已在 Fleet Engine 中建立然後更新工作排序 ,如以下範例所示

DeliveryDriverApi api = DeliveryDriverApi.getInstance();
DeliveryVehicleReporter reporter = api.getDeliveryVehicleReporter();
try {
   List<VehicleStop> stops = reporter.setVehicleStops(
     ImmutableList.of(
         VehicleStop.builder()
             .setVehicleStopState(VehicleStopState.ARRIVED)
             .setWaypoint(Waypoint.builder().setLatLng(37.1749, 122.412).build())
             .setTasks(ImmutableList.of(task1)) // Previously created DeliveryTask in Fleet Engine.
             .build(),
         VehicleStop.builder()
             .setVehicleStopState(VehicleStopState.NEW) // The current vehicle stop.
             .setWaypoint(Waypoint.builder().setLatLng(37.7749, 122.4194).build())
             .setTasks(ImmutableList.of(task2)) // Previously created DeliveryTask in Fleet Engine.
             .build(),
         VehicleStop.builder()
             .setVehicleStopState(VehicleStopState.NEW)
             .setWaypoint(Waypoint.builder().setLatLng(37.3382, 121.8863).build())
             .setTasks(ImmutableList.of(task3, task4)) // Previously created DeliveryTasks in Fleet Engine.
             .build())).get();
   // Successfully updated vehicle stops in Fleet Engine. Returns the successfully set VehicleStops.
} catch (Exception e)  {
   // Failed to update vehicle stops in Fleet Engine. Setting VehicleStops must be attempted again after resolving
   // errors.
}

可能導致無法更新的例外狀況 連結到驅動程式 SDK 的內部狀態如果發生這種情況,請解決問題,然後 再次呼叫 setVehicleStops,直到呼叫成功為止。

可能的問題包括:

  • 指定的 VehicleStops 未遵循有效的圖案。只有第一個 VehicleStop 可位於任何 VehicleStopStates:NEW、ENROUTE 或 ARRIVED。 目前停靠站後方的車輛停靠站必須位於新 VehicleStopState。

  • 工作不存在,或是屬於其他車輛。

  • 車輛不存在。

車輛正在前往下一個停靠站

車輛從停靠站出發,以及何時抵達 Fleet Engine 必須收到通知 開始導航。您可以透過驅動程式 SDK 通知 Fleet Engine。

通知 Fleet Engine 車輛從停靠站出發前,請務必確認 系統已建立並設定車輛停靠站。然後通知 車輛的班次,如以下範例所示。

DeliveryDriverApi api = DeliveryDriverApi.getInstance();
DeliveryVehicleReporter reporter = api.getDeliveryVehicleReporter();
reporter.enableLocationTracking(); // Location tracking must be enabled.

// Create Vehicle, VehicleStops, and DeliveryTasks.
// Set VehicleStops on Vehicle.

navigator.setDestination(vehicleStop.getWaypoint());
try {
   List<VehicleStop> updatedStops = reporter.enrouteToNextStop().get();
   // Successfully updated vehicle stops in Fleet Engine. Returns the set VehicleStops, with the first
   // VehicleStop updated to ENROUTE state.
} catch (Exception e)  {
   // Failed to update vehicle stops in Fleet Engine. Updating VehicleStops must be attempted again
   // after resolving errors.
}

可能會發生例外狀況,可能導致驅動程式 SDK 的更新無法更新 內部狀態。如果發生這種情況,請先解決問題,然後致電 enrouteToNextStop,直到成功為止。

可能的問題包括:

  • 驅動程式 SDK 中未設定剩餘的 VehicleStops

車輛抵達停靠站

車輛抵達停靠站時,Fleet Engine 必須收到通知。你可以 從驅動程式 SDK 通知 Fleet Engine

在通知 Fleet Engine 車輛已抵達停靠站之前,請確定 車輛停靠站已設定。然後通知 Fleet Engine 車輛抵達上車地點 位置,如以下範例所示。

DeliveryDriverApi api = DeliveryDriverApi.getInstance();
DeliveryVehicleReporter reporter = api.getDeliveryVehicleReporter();
reporter.enableLocationTracking(); // Location tracking must be enabled.

// Create Vehicle, VehicleStops, and DeliveryTasks.
// Set VehicleStops on Vehicle.
// Mark ENROUTE to VehicleStop and start guidance using Navigator.

try {
   List<VehicleStop> updatedStopsArrived = reporter.arrivedAtStop().get();
   // Successfully updated vehicle stops in Fleet Engine. Returns the set VehicleStops, with the first
   // VehicleStop updated to ARRIVED state.
   navigator.clearDestinations();
} catch (Exception e)  {
   // Failed to update vehicle stops in Fleet Engine. Updating VehicleStops must be attempted again
   // after resolving errors.
}

可能會發生例外狀況,導致無法更新驅動程式 SDK 的內部 state.如果發生這種情況,請解決問題再重新呼叫 arrivedAtStop 直到成功為止

可能的問題包括:

  • 驅動程式 SDK 中未設定剩餘的 VehicleStops

車輛完成一個停靠站

車輛完成停靠點時,Fleet Engine 必須收到通知。這會導致 設為「關閉」狀態你可以通知機群 來自 Driver SDK 的引擎

通知 Fleet Engine 車輛已完成車輛停靠站,如下所示: 如以下範例所示

DeliveryDriverApi api = DeliveryDriverApi.getInstance();
DeliveryVehicleReporter reporter = api.getDeliveryVehicleReporter();
reporter.enableLocationTracking(); // Location tracking must be enabled.

// After completing the tasks at the VehicleStop, remove it from the
// the current list of VehicleStops.

try {
   List<VehicleStop> updatedStopsCompleted = reporter.completedStop().get();
   // Successfully updated vehicle stops in Fleet Engine. All tasks on the completed stop are set to CLOSED.
   // Returns the set VehicleStops, with the completed VehicleStop removed from the remaining list.
} catch (Exception e)  {
   // Failed to update vehicle stops in Fleet Engine. Updating VehicleStops must be attempted again
   // after resolving errors.
}

可能會發生例外狀況,導致無法更新驅動程式 SDK 的內部 state.如果發生這種情況,請解決問題再重新呼叫 completedStop 直到成功為止

可能的問題包括:

  • 驅動程式 SDK 中未設定剩餘的 VehicleStops

關閉工作

如要關閉已指派給車輛的工作,請通知 Fleet Engine 車輛已完成執行工作的停靠站 或是從車輛停靠站清單中移除方法是設定 其餘車輛停靠站就和 更新車輛的工作排序

如果工作尚未分配到需要關閉的車輛,請更新 轉換為「關閉」狀態但無法重新開啟「已關閉」的工作。

關閉工作並不代表成功或失敗。它表示工作 如要追蹤運送狀態, 指出工作的實際結果,以便顯示交付結果。

你必須將任務指派給車輛,才能使用 Driver SDK 關閉工作。如要關閉已指派給車輛的工作,請通知 車輛已完成工作停靠站的 Fleet Engine。

或者,您也可以更新車輛的工作排序 然後從清單中刪除所需的工作 停靠站。

設定工作結果和結果位置

關閉工作並不表示成功或失敗。它表示工作 如要追蹤運送狀態,你必須 指出工作的實際結果,以便顯示推送結果,以此類推 也應該為這類服務設定適當的收費方式設定後即無法變更 工作結果不過,您可以修改工作結果時間和工作結果位置 設定完成後

處於「已關閉」狀態的工作,可將結果設為 成功或失敗Fleet Engine 只會針對狀態為 成功。

標記工作結果時,Fleet Engine 會自動填入工作結果 最後已知車輛位置的結果位置。您可以覆寫這項設定 呼叫 Fleet Engine請注意,您無法設定工作結果位置 驅動程式 SDK 版本

以下程式碼範例示範如何使用 Driver SDK 設定工作 結果和時間戳記

static final String TASK_ID = "task-8241890";

DeliveryDriverApi api = DeliveryDriverApi.getInstance();
DeliveryTaskManager taskManager = api.getDeliveryTaskManager();

// Updating an existing DeliveryTask which is already CLOSED. Manually
// setting TaskOutcomeLocation with Driver SDK is not supported at this time.
UpdateDeliveryTaskRequest req = UpdateDeliveryTaskRequest.builder(TASK_ID)
    .setTaskOutcome(TaskOutcome.SUCCEEDED)
    .setTaskOutcomeTimestamp(now()) // Timestamp in milliseconds.
    .build();

try {
   DeliveryTask updatedTask = taskManager.updateTask(req);
   // Handle UpdateTaskRequest DeliveryTask response.
} catch (Exception e)  {
   // Handle UpdateTaskRequest error.
}

查詢車輛

您可以透過驅動程式 SDK 查詢車輛。查詢車輛前 確保 初始化 Delivery Driver API。 接下來,您就可以查詢車輛,如以下範例所示。

DeliveryDriverApi api = DeliveryDriverApi.getInstance();
DeliveryVehicleManager vehicleManager = api.getDeliveryVehicleManager();
try {
   DeliveryVehicle vehicle = vehicleManager.getVehicle().get();
   // Handle GetVehicleRequest DeliveryVehicle response.
} catch (Exception e)  {
   // Handle GetVehicleRequest error.
}

DeliveryVehicleManager 只能查詢 DeliveryVehicle