13min

Searching by location - Full example

Example code to search a WebApp by location, and output the results on a map or in a list

Prerequisites

Introduction

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.

Step by Step

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:

Current location (using browser default functionality) and desired search radius

Add HTML and JavaScript

HTML
JS
|

2 - Output the results on the Page in a simple list

Use a Layout of your choice.

{%- include 'webapp', id: '1', use_location_search: 'true', layout: 'default' -%}

3 - Output the results on a map

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.

HTML
|

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.

JS
|

Layout 'json':

JSON
|

Full example

JS
|