There are more AWS SDK examples available in the AWS Doc SDK Examples
There are more AWS SDK examples available in the AWS Doc SDK Examples
The following code examples show you how to use Amazon Location Service with an AWS software development kit (SDK).
Basics are code examples that show you how to perform the essential operations within a service.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
More resources
Amazon Location Developer Guide – More information about Amazon Location.
Amazon Location API Reference – Details about all available Amazon Location actions.
AWS Developer Center
– Code examples that you can filter by category or full-text search. AWS SDK Examples
– GitHub repo with complete code in preferred languages. Includes instructions for setting up and running the code.
Get started
The following code examples show how to get started using Amazon Location Service.
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html * * In addition, you need to create a collection using the AWS Management * console. For information, see the following documentation. * * https://docs.aws.amazon.com/location/latest/developerguide/geofence-gs.html */ public class HelloLocation { private static LocationAsyncClient locationAsyncClient; private static final Logger logger = LoggerFactory.getLogger(HelloLocation.class); // This Singleton pattern ensures that only one `LocationClient` // instance. private static LocationAsyncClient getClient() { if (locationAsyncClient == null) { SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(100) .connectionTimeout(Duration.ofSeconds(60)) .readTimeout(Duration.ofSeconds(60)) .writeTimeout(Duration.ofSeconds(60)) .build(); ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) .apiCallAttemptTimeout(Duration.ofSeconds(90)) .retryStrategy(RetryMode.STANDARD) .build(); locationAsyncClient = LocationAsyncClient.builder() .httpClient(httpClient) .overrideConfiguration(overrideConfig) .build(); } return locationAsyncClient; } public static void main(String[] args) { final String usage = """ Usage: <collectionName> Where: collectionName - The Amazon location collection name. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String collectionName = args[0]; listGeofences(collectionName); } /** * Lists geofences from a specified geofence collection asynchronously. * * @param collectionName The name of the geofence collection to list geofences from. * @return A {@link CompletableFuture} representing the result of the asynchronous operation. * The future completes when all geofences have been processed and logged. */ public static CompletableFuture<Void> listGeofences(String collectionName) { ListGeofencesRequest geofencesRequest = ListGeofencesRequest.builder() .collectionName(collectionName) .build(); ListGeofencesPublisher paginator = getClient().listGeofencesPaginator(geofencesRequest); CompletableFuture<Void> future = paginator.subscribe(response -> { if (response.entries().isEmpty()) { logger.info("No Geofences were found in the collection."); } else { response.entries().forEach(geofence -> logger.info("Geofence ID: " + geofence.geofenceId()) ); } }); return future; } }
-
For API details, see ListGeofencesPaginator in AWS SDK for Java 2.x API Reference.
-
- Kotlin
-
- SDK for Kotlin
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html In addition, you need to create a collection using the AWS Management console. For information, see the following documentation. https://docs.aws.amazon.com/location/latest/developerguide/geofence-gs.html */ suspend fun main(args: Array<String>) { val usage = """ Usage: <colletionName> Where: colletionName - The Amazon location collection name. """ if (args.size != 1) { println(usage) exitProcess(0) } val colletionName = args[0] listGeofences(colletionName) } /** * Lists the geofences for the specified collection name. * * @param collectionName the name of the geofence collection */ suspend fun listGeofences(collectionName: String) { val request = ListGeofencesRequest { this.collectionName = collectionName } LocationClient { region = "us-east-1" }.use { client -> val response = client.listGeofences(request) val geofences = response.entries if (geofences.isNullOrEmpty()) { println("No Geofences found") } else { geofences.forEach { geofence -> println("Geofence ID: ${geofence.geofenceId}") } } } }
-
For API details, see ListGeofencesPaginator
in AWS SDK for Kotlin API reference.
-
Hello Amazon Location Service
The following code examples show how to get started using Amazon Location Service.
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html * * In addition, you need to create a collection using the AWS Management * console. For information, see the following documentation. * * https://docs.aws.amazon.com/location/latest/developerguide/geofence-gs.html */ public class HelloLocation { private static LocationAsyncClient locationAsyncClient; private static final Logger logger = LoggerFactory.getLogger(HelloLocation.class); // This Singleton pattern ensures that only one `LocationClient` // instance. private static LocationAsyncClient getClient() { if (locationAsyncClient == null) { SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(100) .connectionTimeout(Duration.ofSeconds(60)) .readTimeout(Duration.ofSeconds(60)) .writeTimeout(Duration.ofSeconds(60)) .build(); ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) .apiCallAttemptTimeout(Duration.ofSeconds(90)) .retryStrategy(RetryMode.STANDARD) .build(); locationAsyncClient = LocationAsyncClient.builder() .httpClient(httpClient) .overrideConfiguration(overrideConfig) .build(); } return locationAsyncClient; } public static void main(String[] args) { final String usage = """ Usage: <collectionName> Where: collectionName - The Amazon location collection name. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String collectionName = args[0]; listGeofences(collectionName); } /** * Lists geofences from a specified geofence collection asynchronously. * * @param collectionName The name of the geofence collection to list geofences from. * @return A {@link CompletableFuture} representing the result of the asynchronous operation. * The future completes when all geofences have been processed and logged. */ public static CompletableFuture<Void> listGeofences(String collectionName) { ListGeofencesRequest geofencesRequest = ListGeofencesRequest.builder() .collectionName(collectionName) .build(); ListGeofencesPublisher paginator = getClient().listGeofencesPaginator(geofencesRequest); CompletableFuture<Void> future = paginator.subscribe(response -> { if (response.entries().isEmpty()) { logger.info("No Geofences were found in the collection."); } else { response.entries().forEach(geofence -> logger.info("Geofence ID: " + geofence.geofenceId()) ); } }); return future; } }
-
For API details, see ListGeofencesPaginator in AWS SDK for Java 2.x API Reference.
-
- Kotlin
-
- SDK for Kotlin
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** Before running this Kotlin code example, set up your development environment, including your credentials. For more information, see the following documentation topic: https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html In addition, you need to create a collection using the AWS Management console. For information, see the following documentation. https://docs.aws.amazon.com/location/latest/developerguide/geofence-gs.html */ suspend fun main(args: Array<String>) { val usage = """ Usage: <colletionName> Where: colletionName - The Amazon location collection name. """ if (args.size != 1) { println(usage) exitProcess(0) } val colletionName = args[0] listGeofences(colletionName) } /** * Lists the geofences for the specified collection name. * * @param collectionName the name of the geofence collection */ suspend fun listGeofences(collectionName: String) { val request = ListGeofencesRequest { this.collectionName = collectionName } LocationClient { region = "us-east-1" }.use { client -> val response = client.listGeofences(request) val geofences = response.entries if (geofences.isNullOrEmpty()) { println("No Geofences found") } else { geofences.forEach { geofence -> println("Geofence ID: ${geofence.geofenceId}") } } } }
-
For API details, see ListGeofencesPaginator
in AWS SDK for Kotlin API reference.
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html * * In addition, you need to create a collection using the AWS Management * console. For information, see the following documentation. * * https://docs.aws.amazon.com/location/latest/developerguide/geofence-gs.html */ public class HelloLocation { private static LocationAsyncClient locationAsyncClient; private static final Logger logger = LoggerFactory.getLogger(HelloLocation.class); // This Singleton pattern ensures that only one `LocationClient` // instance. private static LocationAsyncClient getClient() { if (locationAsyncClient == null) { SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(100) .connectionTimeout(Duration.ofSeconds(60)) .readTimeout(Duration.ofSeconds(60)) .writeTimeout(Duration.ofSeconds(60)) .build(); ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) .apiCallAttemptTimeout(Duration.ofSeconds(90)) .retryStrategy(RetryMode.STANDARD) .build(); locationAsyncClient = LocationAsyncClient.builder() .httpClient(httpClient) .overrideConfiguration(overrideConfig) .build(); } return locationAsyncClient; } public static void main(String[] args) { final String usage = """ Usage: <collectionName> Where: collectionName - The Amazon location collection name. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String collectionName = args[0]; listGeofences(collectionName); } /** * Lists geofences from a specified geofence collection asynchronously. * * @param collectionName The name of the geofence collection to list geofences from. * @return A {@link CompletableFuture} representing the result of the asynchronous operation. * The future completes when all geofences have been processed and logged. */ public static CompletableFuture<Void> listGeofences(String collectionName) { ListGeofencesRequest geofencesRequest = ListGeofencesRequest.builder() .collectionName(collectionName) .build(); ListGeofencesPublisher paginator = getClient().listGeofencesPaginator(geofencesRequest); CompletableFuture<Void> future = paginator.subscribe(response -> { if (response.entries().isEmpty()) { logger.info("No Geofences were found in the collection."); } else { response.entries().forEach(geofence -> logger.info("Geofence ID: " + geofence.geofenceId()) ); } }); return future; } }
-
For API details, see ListGeofencesPaginator in AWS SDK for Java 2.x API Reference.
-