Adding the new Suggestions API of Amazon Location Service to your website
Today, Amazon Location Service introduces the new Suggestions API which adds autocomplete functionality when searching for places on a map or inserting addresses in a web form. The benefits of this new addition are twofold: it guides users to selecting their intended search string more rapidly and improves the relevance of results by increasing the likelihood of direct matches, leading to an overall better user experience.
Amazon Location Service provides affordable data tracking and geofencing capabilities, and native integrations with AWS services, so you can create sophisticated location-enabled applications quickly, without the high cost of custom development.
Introducing the new Suggestions API
Until today, users could leverage the SearchPlaceIndexForText API and search for free-form text with no autocomplete capabilities included in it. Hence, a user would need to type the precise address in order to get the expected result. Also, this API is built for returning common landmarks, buildings and businesses that contain the search term that was submitted as input, say “fish”.
With the newly launched SearchPlaceIndexForSuggestions API, you start getting suggestions as soon as the first letters are inserted in your search bar (or web form), decreasing the time it takes to identify the intended address, in order of relevance. Continuing the example above, when inserting “fish” to the new API, the first result might be (based on other factors that are explained in the API call syntax below): “Fish Aquarium”, or “Fish Market” as a suggested completion to the word/phrase based on the most popular search criteria. This way, the higher a result is returned from the API, the more likely it will be aligned to what the user was expecting when they started typing.
Using another example, searching for “statue” when using the SearchPlaceIndexForSuggestions API will return the following results:
In order to use the Suggestions API the parameters needed are listed as follows:
- IndexName is the name of the place index resource that has been created in your AWS account and you want to use for the search.
- Text is the address, name, city, or Region to be used in the search.
- BiasPosition searches for results closest to the given position.
More information about these parameters can be found on the API’s documentation page.
Let us show you how get started with a simple demo.
Let us walk you through how to build a very simple web application featuring a map with a search box available to look for places using the newly launched API.
Firstly, let’s get set up by using the AWS Management Console and in particular, Amazon Location Service to create our map.
- You can open the Amazon Location Service Console, and then can either click Try it! to create a set of starter resources, or you can open up the navigation on the left and create them one-by-one. Let’s go for one-by-one, and click Maps:
- Click Create map
- Add a Name and Description to help you differentiate this map from other ones that are already existing or might exist in the future.
- Choose Esri Light from the map selection as this is a good fit for our application.
- You can choose Yes on the Pricing plan use case since this is only going to be using sample location data.
- Finally, tick the very last box before clicking Create map. If you want to know more about the available Pricing plans available on Amazon Location Service, you can have a look at its pricing page.
Almost instantly after that, you can see the newly created map!
Now, let’s create a Place index which supports both “SearchPlaceindexForSuggestions” and “SearchPlaceindexForText” API operations. In the Amazon Location Service menu, choose “Place indexes”.
You can move ahead with creating one by clicking the respective button.
The overall process is very simple; after adding a Name and a Description, you have to choose one of the two supported Data providers (Esri or HERE, we go for Esri similarly to how we chose the Map above):
and then Data storage options and Pricing plans, in the exact same fashion as with the previous map creation form before agreeing with the Terms and Conditions and clicking the button to create the place index.
Now that we have created our Map and Place index, we’re ready to move to our next step which involves Amazon Cognito, the service that will help authenticate users as a secure alternative to directly embedding credentials associated with an AWS Identity and Access Management (IAM) user. In particular, we will see how to configure Amazon Cognito for a website with anonymous users, like the demo application we are about to deploy. More information about using Amazon Cognito to allow access to Amazon Location Service can be found here.
- To get started, open Amazon Cognito from the AWS Console and choose Manage Identity Pools and then Create new identity pool so that you reach the page below:
- In the “Role Summary” section, there is an inline policy that looks like the one below:
This needs to be replaced by a custom one, so that users are able to interact with the applications but also ensure the principle of least privilege is followed:
Note: make sure you replace
- [region] with the region code (e.g. us-east-1 for US East (N. Virginia)),
- [account number] with the unique number of your AWS account, in both occurrences of these above,
- [map name] with the name of the map you created in the first step of the process (this will be “MyMap” if you followed the suggestion of this post)
- [place index] with the name of the place index you crated in the second step of the process (this will be “MyPlaceIndex” if you followed the suggestion of this post).
Note: make sure the “aws:Referer” field matches the domain name of the application (e.g. http://localhost:5000/*), in both occurrences of this above.
- Click next and then you are introduced to a screen that involves a section named “Get AWS credentials”, which you need to expand and copy the text in red (Identity Pool ID) for future usage.
Having set up all 3 items that are needed in order to successfully run our application, namely a Map and a Place index on Amazon Location Service, and an Identity Pool on Amazon Cognito, we are ready to configure the application code which uses MapLibre GL JS with Amazon Location Service and AWS Amplify.
Within index.html, replace <YOUR_COGNITO_IDENTITY_POOL_ID> with the Amazon Cognito Identity Pool ID that you copied earlier. Finally, replace <YOUR_MAP_NAME> with the name of a Map resource that you previously created as well as the <YOUR_PLACE_INDEX_NAME> with the Index name you created, respectively.
Copy and paste the code above in a .html and .js file, respectively and place them under the same directory.
You are now ready to test the application on a web server of your choice, such as by running npx run serve or python3 -m http.server on your terminal.
The expected outcome should be an interface like the one below, in which you can type on the textbox placed on the left hand-side and see suggestions popping up in real time via leveraging the newly introduced “SearchPlaceindexForSuggestions” API which powers this feature.
If you do click on the Search button, then the “SearchPlaceIndexForText” API is called, and places a pointer in the map, showcasing how both API operations can work hand-in-hand on real-life use cases.
That’s it! We have now managed to create a simple web application in which we can get suggestions for places in real time as we type, and see where exactly they are located in the map using Amazon Location Service and its new API.
The new SearchPlaceindexForSuggestions API is available today in all Regions where Amazon Location Service is available today. Find more about it by reading its documentation page.
You can evaluate Amazon Location Service using the free tier during your first three months of request-based usage.