Aktualizowanie lokalizacji pojazdu
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Aby zapewnić optymalne działanie Fleet Engine, przesyłaj do niego strumień aktualizacji lokalizacji pojazdu. Aby przekazać te aktualizacje, możesz użyć jednej z tych metod:
- Użyj pakietu Driver SDK: najprostsza opcja. Zapoznaj się z dokumentacją pakietu Driver SDK na Android lub iOS.
- Użyj kodu niestandardowego: przydatne, jeśli lokalizacje są przekazywane przez Twój backend lub jeśli używasz urządzeń innych niż Android lub iOS. W tym przewodniku opisujemy to podejście.
Jeśli nie używasz pakietu Driver SDK do aktualizowania lokalizacji pojazdu, możesz bezpośrednio wywołać Fleet Engine z lokalizacją pojazdu. W przypadku każdego aktywnego pojazdu Fleet Engine oczekuje aktualizacji lokalizacji co najmniej raz na minutę i co najwyżej raz na 5 sekund. Te aktualizacje wymagają tylko uprawnień użytkownika pakietu sterowników SDK Fleet Engine.
Przykład aktualizacji lokalizacji pojazdu
Java
static final String PROJECT_ID = "project-id";
static final String VEHICLE_ID = "vid-8241890";
VehicleServiceBlockingStub vehicleService = VehicleService.newBlockingStub(channel);
String vehicleName = "providers/" + PROJECT_ID + "/vehicles/" + VEHICLE_ID;
Vehicle updatedVehicle = Vehicle.newBuilder()
.setLastLocation(VehicleLocation.newBuilder()
.setSupplementalLocation(LatLng.newBuilder()
.setLatitude(37.3382)
.setLongitude(121.8863))
.setSupplementalLocationTime(now())
.setSupplementalLocationSensor(LocationSensor.CUSTOMER_SUPPLIED_LOCATION)
.setSupplementalLocationAccuracy(DoubleValue.of(15.0))) // Optional
.build();
UpdateVehicleRequest updateVehicleRequest = UpdateVehicleRequest.newBuilder()
.setName(vehicleName)
.setVehicle(updatedVehicle)
.setUpdateMask(FieldMask.newBuilder()
.addPaths("last_location"))
.build();
try {
Vehicle updatedVehicle =
vehicleService.updateVehicle(updateVehicleRequest);
} catch (StatusRuntimeException e) {
Status s = e.getStatus();
switch (s.getCode()) {
case NOT_FOUND:
// Most implementations will call CreateVehicle in this case
break;
case PERMISSION_DENIED:
break;
}
return;
}
// If no Exception, Vehicle updated successfully.
REST
curl -X PUT \
"https://fleetengine.googleapis.com/v1/providers/project-id/vehicles/vid-8241890?updateMask=last_location" \
-H "Authorization: Bearer $JWT" \
-H "Content-Type: application/json" \
--data-binary @- << EOM
{
"supplementalLocation": {"latitude": 12.1, "longitude": 14.5},
"supplementalLocationTime": "$(date -u --iso-8601=seconds)",
"supplementalLocationSensor": "CUSTOMER_SUPPLIED_LOCATION",
"supplementalLocationAccuracy": 15
}
EOM
Zapoznaj się z dokumentacją providers.vehicles.update.
Co dalej?
O ile nie stwierdzono inaczej, treść tej strony jest objęta licencją Creative Commons – uznanie autorstwa 4.0, a fragmenty kodu są dostępne na licencji Apache 2.0. Szczegółowe informacje na ten temat zawierają zasady dotyczące witryny Google Developers. Java jest zastrzeżonym znakiem towarowym firmy Oracle i jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-08-31 UTC.
[null,null,["Ostatnia aktualizacja: 2025-08-31 UTC."],[[["\u003cp\u003eFleet Engine requires frequent vehicle location updates for optimal performance, achievable through the Driver SDK or custom code.\u003c/p\u003e\n"],["\u003cp\u003eThe Driver SDK is the simplest method for location updates, with dedicated documentation for Android and iOS.\u003c/p\u003e\n"],["\u003cp\u003eFor custom solutions, direct calls to Fleet Engine can be made with location data, requiring updates every 5 seconds to 1 minute.\u003c/p\u003e\n"],["\u003cp\u003eVehicles can be updated using REST or client libraries, with code examples provided for both Java and REST.\u003c/p\u003e\n"]]],[],null,["# Update a vehicle location\n\n| **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\nFor the best performance with Fleet Engine, provide it with a stream of vehicle\nlocation updates. Use either of the following ways to provide these updates:\n\n1. **Use the Driver SDK** : simplest option. See the Driver SDK documentation for [Android](/maps/documentation/mobility/driver-sdk/on-demand/android/minimum-requirements) or [iOS](//maps/documentation/mobility/driver-sdk/on-demand/ios/minimum-requirements).\n2. **Use custom code**: useful if locations are relayed through your backend, or if you use devices other than Android or iOS. This guide covers that approach.\n\nIf not using the Driver SDK to update the vehicle's location, you can make a\ndirect call to Fleet Engine with the vehicle's location. For any active vehicle,\nFleet Engine expects a location update at least once every minute and at most\nonce every 5 seconds. These updates require only *Fleet Engine Driver SDK User*\nprivileges.\n\nUpdate vehicle location example\n-------------------------------\n\n### Java\n\n static final String PROJECT_ID = \"\u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e\";\n static final String VEHICLE_ID = \"\u003cvar translate=\"no\"\u003evid-8241890\u003c/var\u003e\";\n\n VehicleServiceBlockingStub vehicleService = VehicleService.newBlockingStub(channel);\n\n String vehicleName = \"providers/\" + PROJECT_ID + \"/vehicles/\" + VEHICLE_ID;\n Vehicle updatedVehicle = Vehicle.newBuilder()\n .setLastLocation(VehicleLocation.newBuilder()\n .setSupplementalLocation(LatLng.newBuilder()\n .setLatitude(37.3382)\n .setLongitude(121.8863))\n .setSupplementalLocationTime(now())\n .setSupplementalLocationSensor(LocationSensor.CUSTOMER_SUPPLIED_LOCATION)\n .setSupplementalLocationAccuracy(DoubleValue.of(15.0))) // Optional\n .build();\n\n UpdateVehicleRequest updateVehicleRequest = UpdateVehicleRequest.newBuilder()\n .setName(vehicleName)\n .setVehicle(updatedVehicle)\n .setUpdateMask(FieldMask.newBuilder()\n .addPaths(\"last_location\"))\n .build();\n\n try {\n Vehicle updatedVehicle =\n vehicleService.updateVehicle(updateVehicleRequest);\n } catch (StatusRuntimeException e) {\n Status s = e.getStatus();\n switch (s.getCode()) {\n case NOT_FOUND:\n // Most implementations will call CreateVehicle in this case\n break;\n case PERMISSION_DENIED:\n break;\n }\n return;\n }\n // If no Exception, Vehicle updated successfully.\n\n### REST\n\n curl -X PUT \\\n \"https://fleetengine.googleapis.com/v1/providers/\u003cvar translate=\"no\"\u003eproject-id\u003c/var\u003e/vehicles/\u003cvar translate=\"no\"\u003evid-8241890\u003c/var\u003e?updateMask=last_location\" \\\n -H \"Authorization: Bearer $JWT\" \\\n -H \"Content-Type: application/json\" \\\n --data-binary @- \u003c\u003c EOM\n {\n \"supplementalLocation\": {\"latitude\": 12.1, \"longitude\": 14.5},\n \"supplementalLocationTime\": \"$(date -u --iso-8601=seconds)\",\n \"supplementalLocationSensor\": \"CUSTOMER_SUPPLIED_LOCATION\",\n \"supplementalLocationAccuracy\": 15\n }\n EOM\n\nSee [providers.vehicles.update](/maps/documentation/mobility/fleet-engine/reference/trips/rest/v1/providers.vehicles/update) reference.\n\nWhat's next\n-----------\n\n- [Get a vehicle](/maps/documentation/mobility/fleet-engine/essentials/vehicles/on-demand-get-vehicle)"]]