Membuat kendaraan pengiriman untuk tugas terjadwal
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Dokumen ini menjelaskan cara membuat kendaraan dari lingkungan server menggunakan
gRPC atau REST. Anda dapat membuat kendaraan dari Driver SDK, asalkan Anda telah menyediakan aplikasi sebagai lingkungan tepercaya menggunakan kredensial yang sesuai.
Untuk memahami cara menggunakan Driver SDK untuk membuat kendaraan, lihat artikel berikut:
Untuk membuat kendaraan baru dari lingkungan server, buat permintaan
CreateDeliveryVehicle ke Fleet Engine. Gunakan objek
CreateDeliveryVehicleRequest untuk menentukan atribut kendaraan pengiriman baru.
Kolom untuk kendaraan tugas terjadwal
Saat membuat DeliveryVehicle, Anda menetapkan kolom opsional berikut:
attributes
last_location
type
Untuk membuat kendaraan tanpa menetapkan kolom opsional, Anda dapat membiarkan
kolom DeliveryVehicle tidak ditetapkan di CreateDeliveryVehicleRequest.
Buat contoh kendaraan
Anda dapat menggunakan library gRPC Java untuk membuat kendaraan, atau REST.
Java
staticfinalStringPROJECT_ID="my-delivery-co-gcp-project";staticfinalStringVEHICLE_ID="vehicle-8241890";// Avoid auto-incrementing IDs.DeliveryServiceBlockingStubdeliveryService=DeliveryServiceGrpc.newBlockingStub(channel);// Vehicle settingsStringparent="providers/"+PROJECT_ID;DeliveryVehiclevehicle=DeliveryVehicle.newBuilder().addAttributes(DeliveryVehicleAttribute.newBuilder().setKey("route_number").setValue("1"))// Opaque to the Fleet Engine.build();// Vehicle requestCreateDeliveryVehicleRequestcreateVehicleRequest=CreateDeliveryVehicleRequest.newBuilder()// No need for the header.setParent(parent).setDeliveryVehicleId(VEHICLE_ID)// Vehicle ID assigned by the Provider.setDeliveryVehicle(vehicle).build();// Error handling// If Fleet Engine does not have vehicle with that ID and the credentials of the// requestor pass, the service creates the vehicle successfully.try{DeliveryVehiclecreatedVehicle=deliveryService.createDeliveryVehicle(createVehicleRequest);}catch(StatusRuntimeExceptione){Statuss=e.getStatus();switch(s.getCode()){caseALREADY_EXISTS:break;casePERMISSION_DENIED:break;}return;}
REST
Untuk membuat kendaraan dari lingkungan server, lakukan panggilan HTTP REST
ke CreateDeliveryVehicle:
Isi POST mewakili entity DeliveryVehicle yang akan dibuat. Anda dapat
menentukan kolom opsional berikut:
attributes
lastLocation
type
# Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local# environmentcurl-XPOST"https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}"\
-H"Content-type: application/json"\
-H"Authorization: Bearer ${JWT}"\
--data-binary@- << EOM{"attributes":[{"key":"model","value":"sedan"}],"lastLocation":{"location":{"latitude":12.1,"longitude":14.5}}}EOM
Untuk membuat kendaraan tanpa menetapkan kolom apa pun, biarkan isi permintaan POST
kosong. Kendaraan yang baru dibuat kemudian mengekstrak ID kendaraan dari
parameter deliveryVehicleId di URL POST.
Contoh:
# Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local# environmentcurl-XPOST"https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}"\
-H"Content-type: application/json"\
-H"Authorization: Bearer ${JWT}"
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-16 UTC."],[[["\u003cp\u003eThis documentation outlines how to create a vehicle using gRPC or REST from a server environment or the Driver SDK (if provisioned as a trusted environment).\u003c/p\u003e\n"],["\u003cp\u003eWhen creating a vehicle, you can optionally set \u003ccode\u003eattributes\u003c/code\u003e, \u003ccode\u003elast_location\u003c/code\u003e, and \u003ccode\u003etype\u003c/code\u003e fields using the \u003ccode\u003eCreateDeliveryVehicle\u003c/code\u003e request.\u003c/p\u003e\n"],["\u003cp\u003eCreating a vehicle requires a \u003ccode\u003eCreateDeliveryVehicleRequest\u003c/code\u003e object with a defined parent, vehicle ID, and optional vehicle attributes.\u003c/p\u003e\n"],["\u003cp\u003eFleet Engine provides libraries and endpoints for vehicle creation using Java gRPC or REST API calls with necessary authorization.\u003c/p\u003e\n"],["\u003cp\u003eTo avoid errors, do not set read-only or updatable fields when creating the vehicle; these can be modified using \u003ccode\u003eUpdateDeliveryVehicle\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,["| **Note:** **Before constructing a vehicle request** , read the requirements under [Vehicle requests](/maps/documentation/mobility/fleet-engine/essentials/vehicles#vehicle_requests) in the Introduction.\n\nThis document describes how to create a vehicle from a server environment using\neither gRPC or REST. You can create a vehicle from the Driver SDK, provided\nyou have provisioned the app as a trusted environment using the appropriate\ncredentials.\n\nTo understand how to use the Driver SDK to create vehicles, see the following:\n\n- [Driver SDK](/maps/documentation/mobility/driver-sdk/scheduled) for scheduled tasks\n- [Service account roles](/maps/documentation/mobility/fleet-engine/essentials/set-up-fleet/service-accounts) under **Fleet Engine essentials**.\n\nTo create a new vehicle from a server environment, make a\n`CreateDeliveryVehicle` request to Fleet Engine. Use the\n`CreateDeliveryVehicleRequest` object to define the attributes of the new\ndelivery vehicle.\n\nFields for scheduled tasks vehicles\n\nWhen creating a `DeliveryVehicle`, you set the following optional fields:\n\n- `attributes`\n- `last_location`\n- `type`\n\n| **Important:** Don't set any other fields. If you do, Fleet Engine returns an error because those fields are either read-only or can only be updated with a call to `UpdateDeliveryVehicle`.\n\nTo create a vehicle without setting any optional fields, you can leave the\n`DeliveryVehicle` field unset in the `CreateDeliveryVehicleRequest`.\n\nCreate vehicle example\n\nYou can use the [Java gRPC library](/maps/documentation/mobility/fleet-engine/essentials/client-libraries-tasks#java) to create a vehicle, or REST. \n\nJava \n\n static final String PROJECT_ID = \"my-delivery-co-gcp-project\";\n static final String VEHICLE_ID = \"vehicle-8241890\"; // Avoid auto-incrementing IDs.\n\n DeliveryServiceBlockingStub deliveryService =\n DeliveryServiceGrpc.newBlockingStub(channel);\n\n // Vehicle settings\n String parent = \"providers/\" + PROJECT_ID;\n DeliveryVehicle vehicle = DeliveryVehicle.newBuilder()\n .addAttributes(DeliveryVehicleAttribute.newBuilder()\n .setKey(\"route_number\").setValue(\"1\")) // Opaque to the Fleet Engine\n .build();\n\n // Vehicle request\n CreateDeliveryVehicleRequest createVehicleRequest =\n CreateDeliveryVehicleRequest.newBuilder() // No need for the header\n .setParent(parent)\n .setDeliveryVehicleId(VEHICLE_ID) // Vehicle ID assigned by the Provider\n .setDeliveryVehicle(vehicle)\n .build();\n\n // Error handling\n // If Fleet Engine does not have vehicle with that ID and the credentials of the\n // requestor pass, the service creates the vehicle successfully.\n\n try {\n DeliveryVehicle createdVehicle =\n deliveryService.createDeliveryVehicle(createVehicleRequest);\n } catch (StatusRuntimeException e) {\n Status s = e.getStatus();\n switch (s.getCode()) {\n case ALREADY_EXISTS:\n break;\n case PERMISSION_DENIED:\n break;\n }\n return;\n }\n\nREST\n\nTo create a vehicle from a server environment, make an HTTP REST call\nto `CreateDeliveryVehicle`: \n\n POST https://fleetengine.googleapis.com/v1/providers/\u003cproject_id\u003e/deliveryVehicles?deliveryVehicleId=\u003cid\u003e\n\nThe POST body represents the `DeliveryVehicle` entity to be created. You can\nspecify the following optional fields:\n\n- `attributes`\n- `lastLocation`\n- `type`\n\n # Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local\n # environment\n curl -X POST \"https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}\" \\\n -H \"Content-type: application/json\" \\\n -H \"Authorization: Bearer ${JWT}\" \\\n --data-binary @- \u003c\u003c EOM\n {\n \"attributes\": [{\"key\": \"model\", \"value\": \"sedan\"}],\n \"lastLocation\": {\"location\": {\"latitude\": 12.1, \"longitude\": 14.5}}\n }\n EOM\n\nTo create a vehicle without setting any fields, leave the body of the POST\nrequest empty. The newly-created vehicle then extracts a vehicle ID from the\n`deliveryVehicleId` parameter in the POST URL.\n\nExample: \n\n # Set $JWT, $PROJECT_ID, and $VEHICLE_ID in the local\n # environment\n curl -X POST \"https://fleetengine.googleapis.com/v1/providers/${PROJECT_ID}/deliveryVehicles?deliveryVehicleId=${VEHICLE_ID}\" \\\n -H \"Content-type: application/json\" \\\n -H \"Authorization: Bearer ${JWT}\"\n\nWhat's next\n\n- [Update delivery vehicle fields](/maps/documentation/mobility/fleet-engine/essentials/vehicles/scheduled-tasks-vehicle-fields)"]]