Crea un vehículo de entrega para las tareas programadas
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
En este documento, se describe cómo crear un vehículo desde un entorno de servidor con gRPC o REST. Puedes crear un vehículo desde el SDK de Driver, siempre que hayas aprovisionado la app como un entorno de confianza con las credenciales adecuadas.
Para comprender cómo usar el SDK de Driver para crear vehículos, consulta lo siguiente:
Para crear un vehículo nuevo desde un entorno de servidor, realiza una solicitud CreateDeliveryVehicle a Fleet Engine. Usa el objeto CreateDeliveryVehicleRequest para definir los atributos del nuevo vehículo de reparto.
Campos para vehículos de tareas programadas
Cuando creas un DeliveryVehicle, configuras los siguientes campos opcionales:
attributes
last_location
type
Para crear un vehículo sin configurar ningún campo opcional, puedes dejar el campo DeliveryVehicle sin configurar en CreateDeliveryVehicleRequest.
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
Para crear un vehículo desde un entorno de servidor, realiza una llamada a la API de REST de HTTP a CreateDeliveryVehicle:
El cuerpo de la solicitud POST representa la entidad DeliveryVehicle que se creará. Puedes especificar los siguientes campos opcionales:
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
Para crear un vehículo sin configurar ningún campo, deja vacío el cuerpo de la solicitud POST. Luego, el vehículo recién creado extrae un ID del vehículo del parámetro deliveryVehicleId en la URL de POST.
Ejemplo:
# 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}"
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Falta la información que necesito","missingTheInformationINeed","thumb-down"],["Muy complicado o demasiados pasos","tooComplicatedTooManySteps","thumb-down"],["Desactualizado","outOfDate","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Problema con las muestras o los códigos","samplesCodeIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 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)"]]