Approaches
为了让车队引擎发挥最佳性能,请至少每分钟一次(最多每 5 秒一次)向其提供车辆位置更新流。您可以通过以下任一方式提供这些更新:
- 使用驱动程序 SDK:最简单的选项。
- 使用自定义代码:如果位置信息是通过后端中继的,或者您使用的是 Android 或 iOS 以外的设备,则此方法非常有用。本文档介绍了该方法。
无论您以何种方式提供车辆位置信息更新,您的后端都负责在配送车辆前往经停点和到达经停点时更新车队引擎。其中包括仓库本身。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