Work with location search
Add location search functionality on a map
First, make sure you've provisioned a search index resource and configured your app using the instructions in either Configure Location Search or Use existing Amazon Location Service resources and you have already setup displaying a map in your application.
AmplifyMapView
provides built-in location search, search field, and place markers. Follow the AmplifyMapView section on the Maps page to setup the AmplifyMapView
.
Location-based search capabilities
Amplify Geo enables you to search for locations by text, addresses, or geo-coordinates.
Search by text
The Amplify.Geo.searchByText()
API enables you to search for places or points of interest by free-form text, such as an address, name, city, or region.
String searchQuery = "Amazon Go";Amplify.Geo.searchByText(searchQuery, result -> { for (final Place place : result.getPlaces()) { Log.i("MyAmplifyApp", place.toString()); } }, error -> Log.e("MyAmplifyApp", "Failed to search for " + searchQuery, error));
val searchQuery = "Amazon Go"Amplify.Geo.searchByText(searchQuery, { for (place in it.places) { Log.i("MyAmplifyApp", place.toString()) } }, { Log.e("MyAmplifyApp", "Failed to search for $searchQuery", it) })
val searchQuery = "Amazon Go"try { val result = Amplify.Geo.searchByText(searchQuery) for (place in result.places) { Log.i("MyAmplifyApp", place.toString()) }} catch (error: GeoException) { Log.e("MyAmplifyApp", "Failed to search for $searchQuery", it)}
String searchQuery = "Amazon Go";RxAmplify.Geo.searchByText(searchQuery).subscribe( result -> { for (final Place place : result.getPlaces()) { Log.i("MyAmplifyApp", place.toString()); } }, error -> Log.e("MyAmplifyApp", "Failed to search for " + searchQuery, error));
Restrict your search results by specifying following parameters inside GeoSearchByTextOptions
:
countries
- to limit the search results to given countries. Follows ISO Alpha-3 country codes. (defaults to "USA")maxResults
- to limit the maximum result set (defaults to 50)searchArea
near
- to act as the search origination locationwithin
- to limit the area to search inside of
Coordinates position = new Coordinates(47.6153, -122.3384);GeoSearchByTextOptions options = GeoSearchByTextOptions.builder() .maxResults(10) .searchArea(SearchArea.near(position)) .countries(Collections.singletonList(CountryCode.USA)) .build();
val position = Coordinates(47.6153, -122.3384)val options = GeoSearchByTextOptions.builder() .maxResults(10) .searchArea(SearchArea.near(position)) .countries(listOf(CountryCode.USA)) .build()
val position = Coordinates(47.6153, -122.3384)val options = GeoSearchByTextOptions.builder() .maxResults(10) .searchArea(SearchArea.near(position)) .countries(listOf(CountryCode.USA)) .build()
Coordinates position = new Coordinates(47.6153, -122.3384);GeoSearchByTextOptions options = GeoSearchByTextOptions.builder() .maxResults(10) .searchArea(SearchArea.near(position)) .countries(Collections.singletonList(CountryCode.USA)) .build();
Search by coordinates
The Amplify.Geo.searchByCoordinates()
API is a reverse Geocoder that takes a coordinate point and returns information about what it finds at that point on the map.
The returned object is the same shape as Amplify.Geo.searchByText()
API.
Coordinates position = new Coordinates(47.6153, -122.3384);Amplify.Geo.searchByCoordinates(position, result -> { for (final Place place : result.getPlaces()) { Log.i("MyAmplifyApp", place.toString()); } }, error -> Log.e("MyAmplifyApp", "Failed to reverse geocode " + position, error));
val position = Coordinates(47.6153, -122.3384)Amplify.Geo.searchByCoordinates(position, { for (place in it.places) { Log.i("MyAmplifyApp", place.toString()) } }, { Log.e("MyAmplifyApp", "Failed to reverse geocode $position", it) })
val position = Coordinates(47.6153, -122.3384)try { val result = Amplify.Geo.searchByCoordinates(position) for (place in result.places) { Log.i("MyAmplifyApp", place.toString()) }} catch (error: GeoException) { Log.e("MyAmplifyApp", "Failed to reverse geocode $position", error)}
Coordinates position = new Coordinates(47.6153, -122.3384);RxAmplify.Geo.searchByCoordinates(position).subscribe( result -> { for (final Place place : result.getPlaces()) { Log.i("MyAmplifyApp", place.toString()); } }, error -> Log.e("MyAmplifyApp", "Failed to reverse geocode " + position, error));
Restrict your search results by specifying following parameters inside GeoSearchByCoordinatesOptions
:
maxResults
- to limit the maximum result set (defaults to 50)
GeoSearchByCoordinatesOptions options = GeoSearchByCoordinatesOptions.builder() .maxResults(1) .build();
val options = GeoSearchByCoordinatesOptions.builder() .maxResults(1) .build()
val options = GeoSearchByCoordinatesOptions.builder() .maxResults(1) .build()
GeoSearchByCoordinatesOptions options = GeoSearchByCoordinatesOptions.builder() .maxResults(1) .build();