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
- For a full example of creating Android project, please follow the project setup walkthrough
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:
amplify 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:
amplify 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.
? Select which capability you want to add:❯ Map (visualize the geospatial data)
? geo category resources require auth (Amazon Cognito). Do you want to add auth now? (Y/n)❯ yes
? Do you want to use the default authentication and security configuration?❯ Default configuration
? How do you want users to be able to sign in?❯ Username
? Do you want to configure advanced settings?❯ No, I am done.
? Provide a name for the Map:❯ mapResourceName
? Who can access this Map?❯ Authorized and Guest users
? Do you want to configure advanced settings? (y/N)❯ no
? Select which capability you want to add:❯ Location search (search by places, addresses, coordinates)
? geo category resources require auth (Amazon Cognito). Do you want to add auth now? (Y/n)❯ yes
? Do you want to use the default authentication and security configuration?❯ Default configuration
? How do you want users to be able to sign in?❯ Username
? Do you want to configure advanced settings?❯ No, I am done.
? Provide a name for the location search index (place index):❯ placeIndexResourceName
? Who can access this search index?❯ Authorized and Guest users
? Do you want to configure advanced settings? (y/N)❯ no
The add
command automatically creates the backend configuration. Once all your configuration is complete, run the following (this might take a few minutes):
amplify 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:
dependencies { implementation 'com.amplifyframework:aws-auth-cognito:ANDROID_VERSION' implementation 'com.amplifyframework:aws-geo-location:ANDROID_VERSION'}
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:
Amplify.addPlugin(new AWSCognitoAuthPlugin());Amplify.addPlugin(new AWSLocationGeoPlugin());Amplify.configure(getApplicationContext());
Your class will look like this:
public class MyAmplifyApp extends Application { @Override public void onCreate() { super.onCreate();
try { Amplify.addPlugin(new AWSCognitoAuthPlugin()); Amplify.addPlugin(new AWSLocationGeoPlugin()); Amplify.configure(getApplicationContext()); Log.i("MyAmplifyApp", "Initialized Amplify"); } catch (AmplifyException error) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error); } }}
Amplify.addPlugin(AWSCognitoAuthPlugin())Amplify.addPlugin(AWSLocationGeoPlugin())Amplify.configure(applicationContext)
Your class will look like this:
class MyAmplifyApp : Application() { override fun onCreate() { super.onCreate()
try { Amplify.addPlugin(AWSCognitoAuthPlugin()) Amplify.addPlugin(AWSLocationGeoPlugin()) Amplify.configure(applicationContext) Log.i("MyAmplifyApp", "Initialized Amplify") } catch (error: AmplifyException) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error) } }}
RxAmplify.addPlugin(new AWSCognitoAuthPlugin());RxAmplify.addPlugin(new AWSLocationGeoPlugin());RxAmplify.configure(getApplicationContext());
Your class will look like this:
public class MyAmplifyApp extends Application { @Override public void onCreate() { super.onCreate();
try { RxAmplify.addPlugin(new AWSCognitoAuthPlugin()); RxAmplify.addPlugin(new AWSLocationGeoPlugin()); RxAmplify.configure(getApplicationContext()); Log.i("MyAmplifyApp", "Initialized Amplify"); } catch (AmplifyException error) { Log.e("MyAmplifyApp", "Could not initialize Amplify", error); } }}
Upon building and running this application you should see the following in your console window:
Initialized Amplify