Add address autocomplete and interactive maps to Adaptive Forms
Enhance the address entry experience in Adaptive Forms with real-time address suggestions and interactive map visualization. As users type an address, matching suggestions appear, allowing them to select an address and visually confirm its location on a map.
This capability is implemented using a reusable client library that enhances the existing Text Input Core Component without requiring a custom component.
Demonstration
The following demonstration illustrates how citizens can report a neighborhood issue by entering an address and confirming its location on an interactive map.
Prerequisites
- AEM Forms as a Cloud Service.
- Adaptive Forms based on Core Components.
- An existing AEM Maven project.
- Familiarity with AEM client libraries.
Overview of the implementation
The client library integrates the following technologies:
- Photon - Retrieves address suggestions and geographic coordinates.
- Leaflet - Renders the interactive map and location marker.
- OpenStreetMap - Provides map data and tiles.
- GuideBridge API - Updates the Adaptive Forms data model with the selected address.
The JavaScript detects the configured Text Input component, retrieves address suggestions as the user types, and displays the corresponding location on a map when an address is selected.
Download the client library
Download or clone the sample client library from the GitHub repository.
Copy the clientlib-locationpicker folder into your AEM project’s client libraries directory under the ui.apps module.
The client library contains the following files:
clientlib-locationpicker/
├── .content.xml
├── js.txt
├── css.txt
├── js/
│ └── locationpicker.js
└── css/
└── locationpicker.css
Configure the client library
Open the locationpicker.js file and locate the configuration object.
const config = {
countryCode: "US",
minCharacters: 3,
maxSuggestions: 5,
debounceDelay: 400,
mapZoom: 17,
mapHeight: 300,
showMap: true,
selector: 'input[name="address"]',
somExpression: "$form.address"
};
Modify the configuration properties as needed.
countryCodeminCharactersmaxSuggestionsdebounceDelaymapZoommapHeightshowMapselectorsomExpressioncountryCode property to restrict address suggestions to another country.Include the client library
The sample client library uses the following category:
aemforms.locationpicker
Include this category in your Adaptive Form’s client library configuration or add it to the embed property of your project’s existing base client library.
Deploy the updated project to your AEM Forms as a Cloud Service environment using Cloud Manager.
Configure the Adaptive Form
Create an Adaptive Form based on Core Components and add a Text Input component with the following properties:
The sample assumes that the address field is at the root of the Adaptive Form, with a SOM expression of $form.address.
selector and somExpression properties in the JavaScript configuration accordingly.Test the functionality
- Preview the Adaptive Form.
- Start typing an address in the configured Text Input field.
- Select an address from the suggested results.
- Verify that the selected address populates the field and an interactive map displays the corresponding location.
- Select a different address and verify that the map and location marker update automatically.
Verify the Adaptive Forms data model
The client library uses the GuideBridge API to update the Adaptive Forms data model with the selected address.
To verify the field value, open the browser developer console and execute the following command:
guideBridge.resolveNode("$form.address").value
The returned value should contain the complete selected address.
Submit the form using sample data and verify that the selected address is included in the submitted data.
Production considerations
The sample uses publicly available address lookup and map services for demonstration purposes.
Before deploying this capability to production, consider the following:
- Review the Photon usage policy.
- Review the OpenStreetMap tile usage policy.
- Evaluate external service availability and usage limits.
- Review privacy and personal data handling requirements.
- Configure the appropriate Content Security Policy directives.
- Consider bundling Leaflet within your AEM application.
- Verify accessibility and keyboard navigation.