Our GeoJSON update for WebApps will work with both newly created WebApps, and WebApps that have been updated after the release of this update.
To apply the update to an existing WebApp, simply go to the WebApp Builder view, and click 'Save'.
Editing the WebApp item
Webapp items have 2 new fields by default:
location - GeoJSON - Stores the location value of your item as coordinates - [long, lat]
address - Text - Stores the human-readable address value of your item
To set these values in Siteglide you will see the new 'Location' tab on the WebApp item edit view. Here you have 2 choices:
Search for an address and then select it from the results dropdown - Recommended
Manually enter the Address and Longitude/Latitude values for your item
Querying the data
Our new Location Search feature relies on 2 URL parameters to work:
longlat - LongLat - The coordinate value of the center point for the search - long,lat
distance - Int - The number of kilometres from the center point
Example - /map?longlat=-1.2660643,51.3926564&distance=18
To display results from these parameters you will need the new use_location_search parameter on your WebApp include:
use_location_search - 'true' or 'false' - default: 'false'
Example - {%- include 'webapp', id: '1', use_location_search: 'true' -%}
Note - Location Search `(uselocationsearch) and Text Search(usesearch`) will not work together at the same time_
Example
Example code to search a WebApp by location, and output the results on a map or in a list.
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.
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
<script> function getMyLocation() {event.preventDefault();if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(showMyPosition); }else{alert("Geolocation is not supported by this browser."); }functionshowMyPosition(position){ var distance = document.querySelector('#current_locations_distance').value ? document.querySelector('#current_locations_distance').value : 10;
window.location.replace(window.location.origin+"/store-locations?longlat="+position.coords.longitude+","+position.coords.latitude+"&distance="+distance);
} }</script>
:::
2 - Output the results on the Page in a simple list
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.
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.