D'autres exemples de AWS SDK sont disponibles dans le référentiel AWS Doc SDK Examples GitHub .
Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
Utilisation GetDevicePosition avec un AWS SDK
Les exemples de code suivants illustrent comment utiliser GetDevicePosition.
Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action en contexte dans l’exemple de code suivant :
- Java
-
- SDK pour Java 2.x
-
/**
* Retrieves the position of a device using the provided LocationClient.
*
* @param trackerName The name of the tracker associated with the device.
* @param deviceId The ID of the device to retrieve the position for.
* @throws RuntimeException If there is an error fetching the device position.
*/
public CompletableFuture<GetDevicePositionResponse> getDevicePosition(String trackerName, String deviceId) {
GetDevicePositionRequest request = GetDevicePositionRequest.builder()
.trackerName(trackerName)
.deviceId(deviceId)
.build();
return getClient().getDevicePosition(request)
.whenComplete((response, exception) -> {
if (exception != null) {
Throwable cause = exception.getCause();
if (cause instanceof ResourceNotFoundException) {
throw new CompletionException("The AWS resource was not found: " + cause.getMessage(), cause);
}
throw new CompletionException("Error fetching device position: " + exception.getMessage(), exception);
}
});
}
- JavaScript
-
- SDK pour JavaScript (v3)
-
import { fileURLToPath } from "node:url";
import {
GetDevicePositionCommand,
LocationClient,
ResourceNotFoundException,
} from "@aws-sdk/client-location";
import data from "./inputs.json" with { type: "json" };
const region = "eu-west-1";
export const main = async () => {
const locationClient = new LocationClient({ region: region });
const deviceId = `${data.inputs.deviceId}`;
const trackerName = `${data.inputs.trackerName}`;
const devicePositionParams = {
DeviceId: deviceId,
TrackerName: trackerName,
};
try {
const command = new GetDevicePositionCommand(devicePositionParams);
const response = await locationClient.send(command);
//state.position = response.position;
console.log("Successfully fetched device position: ", response);
} catch (error) {
console.log("Error ", error);
/* if (caught instanceof ResourceNotFoundException) {
console.error(
`"The resource was not found: ${caught.message} \n Exiting program.`,
);
} else {
`An unexpected error error occurred: ${caught.message} \n Exiting program.`;
}
return;*/
}
};
- Kotlin
-
- SDK pour Kotlin
-
/**
* Retrieves the position of a device using the provided LocationClient.
*
* @param trackerName The name of the tracker associated with the device.
* @param deviceId The ID of the device to retrieve the position for.
*/
suspend fun getDevicePosition(trackerName: String, deviceId: String): GetDevicePositionResponse {
val request = GetDevicePositionRequest {
this.trackerName = trackerName
this.deviceId = deviceId
}
LocationClient.fromEnvironment { region = "us-east-1" }.use { client ->
return client.getDevicePosition(request)
}
}