更新送货车辆位置

Approaches

为了让 Fleet Engine 达到最佳性能,请为其提供一流车辆 位置至少每分钟更新一次,最多每 5 秒更新一次。 使用以下任一方法提供这些更新:

  • 使用驱动程序 SDK:最简单的方法。
  • 使用自定义代码:如果营业地点是通过后端中继的,则此选项很有用。 或者使用的是 Android 或 iOS 以外的设备本文档介绍了 方法。

无论您通过何种方式提供车辆位置信息更新, 负责在有货车辆正在前往 以及到达经停点时这包括该仓库本身。舰队引擎 不会自动检测这些事件。

更新车辆位置示例

您可以使用 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

后续步骤