Page updated Jan 16, 2024

Work with location search

Amplify Android v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Android to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for Android, you can access the documentation here.

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.

import android.util.Log; import com.amplifyframework.core.Amplify; import com.amplifyframework.geo.models.Place;
1import android.util.Log;
2import com.amplifyframework.core.Amplify;
3import com.amplifyframework.geo.models.Place;
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) );
1String searchQuery = "Amazon Go";
2Amplify.Geo.searchByText(searchQuery,
3 result -> {
4 for (final Place place : result.getPlaces()) {
5 Log.i("MyAmplifyApp", place.toString());
6 }
7 },
8 error -> Log.e("MyAmplifyApp", "Failed to search for " + searchQuery, error)
9);
import android.util.Log import com.amplifyframework.core.Amplify
1import android.util.Log
2import com.amplifyframework.core.Amplify
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) } )
1val searchQuery = "Amazon Go"
2Amplify.Geo.searchByText(searchQuery,
3 {
4 for (place in it.places) {
5 Log.i("MyAmplifyApp", place.toString())
6 }
7 },
8 { Log.e("MyAmplifyApp", "Failed to search for $searchQuery", it) }
9)

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 location
    • within - to limit the area to search inside of
import com.amplifyframework.geo.models.Coordinates; import com.amplifyframework.geo.models.CountryCode; import com.amplifyframework.geo.models.SearchArea; import com.amplifyframework.geo.options.GeoSearchByTextOptions; import java.util.Collections;
1import com.amplifyframework.geo.models.Coordinates;
2import com.amplifyframework.geo.models.CountryCode;
3import com.amplifyframework.geo.models.SearchArea;
4import com.amplifyframework.geo.options.GeoSearchByTextOptions;
5import java.util.Collections;
Coordinates position = new Coordinates(47.6153, -122.3384); GeoSearchByTextOptions options = GeoSearchByTextOptions.builder() .maxResults(10) .searchArea(SearchArea.near(position)) .countries(Collections.singletonList(CountryCode.USA)) .build();
1Coordinates position = new Coordinates(47.6153, -122.3384);
2GeoSearchByTextOptions options = GeoSearchByTextOptions.builder()
3 .maxResults(10)
4 .searchArea(SearchArea.near(position))
5 .countries(Collections.singletonList(CountryCode.USA))
6 .build();
import com.amplifyframework.geo.models.Coordinates import com.amplifyframework.geo.models.CountryCode import com.amplifyframework.geo.models.SearchArea import com.amplifyframework.geo.options.GeoSearchByTextOptions
1import com.amplifyframework.geo.models.Coordinates
2import com.amplifyframework.geo.models.CountryCode
3import com.amplifyframework.geo.models.SearchArea
4import com.amplifyframework.geo.options.GeoSearchByTextOptions
val position = Coordinates(47.6153, -122.3384) val options = GeoSearchByTextOptions.builder() .maxResults(10) .searchArea(SearchArea.near(position)) .countries(listOf(CountryCode.USA)) .build()
1val position = Coordinates(47.6153, -122.3384)
2val options = GeoSearchByTextOptions.builder()
3 .maxResults(10)
4 .searchArea(SearchArea.near(position))
5 .countries(listOf(CountryCode.USA))
6 .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.

import android.util.Log; import com.amplifyframework.core.Amplify; import com.amplifyframework.geo.models.Coordinates; import com.amplifyframework.geo.models.Place;
1import android.util.Log;
2import com.amplifyframework.core.Amplify;
3import com.amplifyframework.geo.models.Coordinates;
4import com.amplifyframework.geo.models.Place;
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) );
1Coordinates position = new Coordinates(47.6153, -122.3384);
2Amplify.Geo.searchByCoordinates(position,
3 result -> {
4 for (final Place place : result.getPlaces()) {
5 Log.i("MyAmplifyApp", place.toString());
6 }
7 },
8 error -> Log.e("MyAmplifyApp", "Failed to reverse geocode " + position, error)
9);
import android.util.Log import com.amplifyframework.core.Amplify import com.amplifyframework.geo.models.Coordinates
1import android.util.Log
2import com.amplifyframework.core.Amplify
3import com.amplifyframework.geo.models.Coordinates
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) } )
1val position = Coordinates(47.6153, -122.3384)
2Amplify.Geo.searchByCoordinates(position,
3 {
4 for (place in it.places) {
5 Log.i("MyAmplifyApp", place.toString())
6 }
7 },
8 { Log.e("MyAmplifyApp", "Failed to reverse geocode $position", it) }
9)

Restrict your search results by specifying following parameters inside GeoSearchByCoordinatesOptions:

  • maxResults - to limit the maximum result set (defaults to 50)
import com.amplifyframework.geo.options.GeoSearchByCoordinatesOptions;
1import com.amplifyframework.geo.options.GeoSearchByCoordinatesOptions;
GeoSearchByCoordinatesOptions options = GeoSearchByCoordinatesOptions.builder() .maxResults(1) .build();
1GeoSearchByCoordinatesOptions options = GeoSearchByCoordinatesOptions.builder()
2 .maxResults(1)
3 .build();
import com.amplifyframework.geo.options.GeoSearchByCoordinatesOptions
1import com.amplifyframework.geo.options.GeoSearchByCoordinatesOptions
val options = GeoSearchByCoordinatesOptions.builder() .maxResults(1) .build()
1val options = GeoSearchByCoordinatesOptions.builder()
2 .maxResults(1)
3 .build()