There are more AWS SDK examples available in the AWS Doc SDK Examples
The following code example shows how to use SearchText
.
- 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
. /** * Searches for a place using the provided search query and prints the detailed information of the first result. * * @param searchQuery the search query to be used for the place search (ex, coffee shop) */ public CompletableFuture<Void> searchText(String searchQuery) { double latitude = 37.7749; // San Francisco double longitude = -122.4194; List<Double> queryPosition = List.of(longitude, latitude); SearchTextRequest request = SearchTextRequest.builder() .queryText(searchQuery) .biasPosition(queryPosition) .build(); return getGeoPlacesClient().searchText(request) .thenCompose(response -> { if (response.resultItems().isEmpty()) { logger.info("No places found."); return CompletableFuture.completedFuture(null); } // Get the first place ID String placeId = response.resultItems().get(0).placeId(); logger.info("Found Place with id: " + placeId); // Fetch detailed info using getPlace GetPlaceRequest getPlaceRequest = GetPlaceRequest.builder() .placeId(placeId) .build(); return getGeoPlacesClient().getPlace(getPlaceRequest) .thenAccept(placeResponse -> { logger.info("Detailed Place Information:"); logger.info("Name: " + placeResponse.placeType().name()); logger.info("Address: " + placeResponse.address().label()); if (placeResponse.foodTypes() != null && !placeResponse.foodTypes().isEmpty()) { logger.info("Food Types:"); placeResponse.foodTypes().forEach(foodType -> { logger.info(" - " + foodType); }); } else { logger.info("No food types available."); } logger.info("-------------------------"); }); }) .exceptionally(exception -> { Throwable cause = exception.getCause(); if (cause instanceof software.amazon.awssdk.services.geoplaces.model.ValidationException) { throw new CompletionException("A validation error occurred: " + cause.getMessage(), cause); } throw new CompletionException("Error performing place search", exception); }); }
-
For API details, see SearchText in AWS SDK for Java 2.x API Reference.
-