Page updated Jan 16, 2024

Set up Amplify Geo

Amplify Geo provides APIs and map UI components for mobile app development such that you can add maps to your app in just a few lines of code. Amplify Geo APIs are powered by Amazon Location Service and the map UI components from MapLibre are already integrated with the Geo APIs. You can quickly get started using Amplify CLI to provision your map resources.

Follow this guide to get started with Amplify Geo through the Amplify CLI.

Note:

  • If you want to use existing Amazon Location Service resources follow this guide instead.
  • If you want to use Amazon Location Service APIs not directly supported by Geo, use the escape hatch to access the Amazon Location Service SDK.

Prerequisites

  • An Android application targeting at least Android SDK API level 24 with Amplify libraries integrated

Provisioning resources through CLI

Prerequisite: Install and configure the Amplify CLI

To start provisioning Geo resources in the backend, go to your project directory and execute the command:

1amplify init

The above command will guide you through setting up your project name and preferred authentication profile. Refer to "Create your application" section for detailed project setup guide.

Now you are able to add a geo resource, such as map resources or a search index:

1amplify add geo

The CLI will let you configure the Geo category based on the capabilities you want to add (maps and/or search). You can either choose to stick with the defaults or configure advanced settings. Please refer to Amplify CLI Geo docs for more details on these configurations.

1? Select which capability you want to add:
2❯ Map (visualize the geospatial data)
3
4? geo category resources require auth (Amazon Cognito). Do you want to add auth now? (Y/n)
5❯ yes
6
7? Do you want to use the default authentication and security configuration?
8❯ Default configuration
9
10? How do you want users to be able to sign in?
11❯ Username
12
13? Do you want to configure advanced settings?
14❯ No, I am done.
15
16? Provide a name for the Map:
17❯ mapResourceName
18
19? Who can access this Map?
20❯ Authorized and Guest users
21
22? Do you want to configure advanced settings? (y/N)
23❯ no
1? Select which capability you want to add:
2❯ Location search (search by places, addresses, coordinates)
3
4? geo category resources require auth (Amazon Cognito). Do you want to add auth now? (Y/n)
5❯ yes
6
7? Do you want to use the default authentication and security configuration?
8❯ Default configuration
9
10? How do you want users to be able to sign in?
11❯ Username
12
13? Do you want to configure advanced settings?
14❯ No, I am done.
15
16? Provide a name for the location search index (place index):
17❯ placeIndexResourceName
18
19? Who can access this search index?
20❯ Authorized and Guest users
21
22? Do you want to configure advanced settings? (y/N)
23❯ no

The add command automatically creates the backend configuration. Once all your configuration is complete, run the following (this might take a few minutes):

1amplify push

A file named amplifyconfiguration.json that contains all geo-related configuration information will be created in your app's src/main/res/raw directory.

Install Amplify Libraries

Add the following dependencies to your build.gradle (Module :app) file and click "Sync Now" when prompted:

1dependencies {
2 implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_VERSION'
3 implementation 'com.amplifyframework:aws-geo-location:ANDROID_VERSION'
4}

Note: The Geo plugin has a dependency on Cognito Auth.

Initialize Amplify Geo

To initialize Amplify Geo, use the Amplify.addPlugin() method to add the AWS Location Geo plugin. Next, finish configuring Amplify by calling Amplify.configure().

Add the following code to your onCreate() method in your application class:

1Amplify.addPlugin(new AWSCognitoAuthPlugin());
2Amplify.addPlugin(new AWSLocationGeoPlugin());
3Amplify.configure(getApplicationContext());

Your class will look like this:

1public class MyAmplifyApp extends Application {
2 @Override
3 public void onCreate() {
4 super.onCreate();
5
6 try {
7 Amplify.addPlugin(new AWSCognitoAuthPlugin());
8 Amplify.addPlugin(new AWSLocationGeoPlugin());
9 Amplify.configure(getApplicationContext());
10 Log.i("MyAmplifyApp", "Initialized Amplify");
11 } catch (AmplifyException error) {
12 Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
13 }
14 }
15}
1Amplify.addPlugin(AWSCognitoAuthPlugin())
2Amplify.addPlugin(AWSLocationGeoPlugin())
3Amplify.configure(applicationContext)

Your class will look like this:

1class MyAmplifyApp : Application() {
2 override fun onCreate() {
3 super.onCreate()
4
5 try {
6 Amplify.addPlugin(AWSCognitoAuthPlugin())
7 Amplify.addPlugin(AWSLocationGeoPlugin())
8 Amplify.configure(applicationContext)
9 Log.i("MyAmplifyApp", "Initialized Amplify")
10 } catch (error: AmplifyException) {
11 Log.e("MyAmplifyApp", "Could not initialize Amplify", error)
12 }
13 }
14}
1RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
2RxAmplify.addPlugin(new AWSLocationGeoPlugin());
3RxAmplify.configure(getApplicationContext());

Your class will look like this:

1public class MyAmplifyApp extends Application {
2 @Override
3 public void onCreate() {
4 super.onCreate();
5
6 try {
7 RxAmplify.addPlugin(new AWSCognitoAuthPlugin());
8 RxAmplify.addPlugin(new AWSLocationGeoPlugin());
9 RxAmplify.configure(getApplicationContext());
10 Log.i("MyAmplifyApp", "Initialized Amplify");
11 } catch (AmplifyException error) {
12 Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
13 }
14 }
15}

Upon building and running this application you should see the following in your console window:

1Initialized Amplify