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 CreateGeofenceCollection avec un AWS SDK
Les exemples de code suivants illustrent comment utiliser CreateGeofenceCollection.
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
-
/**
* Creates a new geofence collection.
*
* @param collectionName the name of the geofence collection to be created
*/
public CompletableFuture<String> createGeofenceCollection(String collectionName) {
CreateGeofenceCollectionRequest collectionRequest = CreateGeofenceCollectionRequest.builder()
.collectionName(collectionName)
.description("Created by using the AWS SDK for Java")
.build();
return getClient().createGeofenceCollection(collectionRequest)
.whenComplete((response, exception) -> {
if (exception != null) {
Throwable cause = exception.getCause();
if (cause instanceof ConflictException) {
throw new CompletionException("The geofence collection was not created due to ConflictException.", cause);
}
throw new CompletionException("Failed to create geofence collection: " + exception.getMessage(), exception);
}
})
.thenApply(response -> response.collectionArn()); // Return only the ARN
}
- JavaScript
-
- SDK pour JavaScript (v3)
-
import { fileURLToPath } from "node:url";
import {
ConflictException,
CreateGeofenceCollectionCommand,
LocationClient,
} from "@aws-sdk/client-location";
import data from "./inputs.json" with { type: "json" };
const region = "eu-west-1";
export const main = async () => {
const geoFenceCollParams = {
CollectionName: `${data.inputs.collectionName}`,
};
const locationClient = new LocationClient({ region: region });
try {
const command = new CreateGeofenceCollectionCommand(geoFenceCollParams);
const response = await locationClient.send(command);
console.log(
"Collection created. Collection name is: ",
response.CollectionName,
);
} catch (caught) {
if (caught instanceof ConflictException) {
console.error("A conflict occurred. Exiting program.");
return;
}
}
};
- Kotlin
-
- SDK pour Kotlin
-
/**
* Creates a new geofence collection.
*
* @param collectionName the name of the geofence collection to be created
*/
suspend fun createGeofenceCollection(collectionName: String): String {
val collectionRequest = CreateGeofenceCollectionRequest {
this.collectionName = collectionName
description = "Created by using the AWS SDK for Kotlin"
}
LocationClient.fromEnvironment { region = "us-east-1" }.use { client ->
val response = client.createGeofenceCollection(collectionRequest)
return response.collectionArn
}
}