Searching by location - Full example
Example code to search a WebApp by location, and output the results on a map or in a list
- You have WebApp items prepared with location data
In this example we'll show you how to add a location search to a Page, and then output the results of that search both in a list and on a map. Items will display in the result set when they are within the radius of a chosen location. One example of how you can use this is as a Store Locator for a franchise of Shops.
1 - Add an HTML Form and JavaScript to take User input to decide starting position and search radius
Two different options are provided in this example:
Add HTML and JavaScript
Use a Layout of your choice.
{%- include 'webapp', id: '1', use_location_search: 'true', layout: 'default' -%}
This relies on there being a layout named 'json'. See the contents of 'json' in the 2nd block of code. Add HTML + Liquid The Liquid makes sure the map is only outputted after the Page has been refreshed by the JavaScript and the correct parameters are available in the URL for filtering the results.
Add JavaScript The following function is triggered by the script from step 1, only after it has finished loading- this occurs because the function's name is referenced in the URL parameter fetching the Script. The code must be placed above the <script> tag from step 1.
Layout 'json':