更新交車地點

Approaches

為確保 Fleet Engine 發揮最佳效能,請提供車流 位置至少每分鐘更新一次,最多每 5 秒更新一次。 請使用下列任一方式提供這些更新:

  • 使用驅動程式 SDK:最簡單的選項。
  • 使用自訂程式碼:如果位置是透過後端轉送,就很適合使用。 以及 Android 或 iOS 以外的裝置本文件將說明 。

無論您以何種方式提供車輛位置更新,後端 負責更新 Fleet Engine 的車輛位置 並抵達該停靠站。這包括庫帳本身。Fleet Engine 不會自動偵測這些事件。

更新車輛位置範例

您可以使用 Java gRPC 程式庫更新機群中的車輛位置 或使用 REST

Java

  static final String PROJECT_ID = "my-delivery-co-gcp-project";
  static final String VEHICLE_ID = "vehicle-8241890";

  DeliveryServiceBlockingStub deliveryService =
    DeliveryServiceGrpc.newBlockingStub(channel);

  // Vehicle settings
  String vehicleName = "providers/" + PROJECT_ID + "/deliveryVehicles/" + VEHICLE_ID;
  DeliveryVehicle myDeliveryVehicle = DeliveryVehicle.newBuilder()
      .setLastLocation(DeliveryVehicleLocation.newBuilder()
          .setSupplementalLocation(LatLng.newBuilder()
              .setLatitude(37.3382)
              .setLongitude(121.8863))
          .setSupplementalLocationTime(now())
          .setSupplementalLocationSensor(DeliveryVehicleLocationSensor.CUSTOMER_SUPPLIED_LOCATION)
          .setSupplementalLocationAccuracy(DoubleValue.of(15.0)))  // Optional
      .build();

  // DeliveryVehicle request
  UpdateDeliveryVehicleRequest updateDeliveryVehicleRequest =
    UpdateDeliveryVehicleRequest.newBuilder()  // No need for the header
        .setName(vehicleName)
        .setDeliveryVehicle(myDeliveryVehicle)
        .setUpdateMask(FieldMask.newBuilder()
            .addPaths("last_location"))
        .build();

  try {
    DeliveryVehicle updatedDeliveryVehicle =
        deliveryService.updateDeliveryVehicle(updateDeliveryVehicleRequest);
  } catch (StatusRuntimeException e) {
    Status s = e.getStatus();
    switch (s.getCode()) {
       case NOT_FOUND:
         break;
       case PERMISSION_DENIED:
         break;
    }
    return;
  }

REST

PATCH https://fleetengine.googleapis.com/v1/providers/<project_id>/deliveryVehicles/<id>?updateMask=last_location

要求詳細資料

要求主體必須包含 DeliveryVehicle 實體,並指定下列欄位:

  • 必填欄位:

    欄位
    lastLocation.supplementalLocation 車輛的位置。
    lastLocation.supplementalLocationTime 車輛上次停在這個地點的時間戳記。
    lastLocation.supplementalLocationSensor 應填入 CUSTOMER_SUPPLIED_LOCATION

  • 選用欄位:

    欄位
    lastLocation.supplementalLocationAccuracy 所提供位置的精確度 (以公尺為單位)。

  # Set JWT, PROJECT_ID, VEHICLE_ID, TASK1_ID, and TASK2_ID in the local
  # environment
  curl -X PATCH "https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles/${VEHICLE_ID}?updateMask=remainingVehicleJourneySegments" \
    -H "Content-type: application/json" \
    -H "Authorization: Bearer ${JWT}" \
    --data-binary @- << EOM
  {
    "lastLocation": {
      "supplementalLocation": {"latitude": 12.1, "longitude": 14.5},
      "supplementalLocationTime": "$(date -u --iso-8601=seconds)",
      "supplementalLocationSensor": "CUSTOMER_SUPPLIED_LOCATION",
      "supplementalLocationAccuracy": 15
    }
  }
  EOM

後續步驟