A complete store locator on one page: a search form (with use my location), the result list with distance and paging, and the results on a map. The search is in the query string (method="get"), so a search can be shared and the page cached.
It uses a channel stores with a Maps Locator (or Maps Fieldtype) field, a category group for the store types (here group 3) and a checkboxes field services with the options wifi, parking and wheelchair. Place the template at stores/index.
Place the map and the list next to each other with some CSS, e.g. in the <head>. The map tag outputs a skip link, the map and the target of the skip link (an empty <span class="maps-skip-target">) before the list, so keep that target out of the grid, or it takes a column of its own:
With width="100%" on the map tag the map fills its column.
Notes:
- The map fits around its markers (
fit_map:markers="yes"): the results and the searched location. How far it zooms in depends on the radius, with a large radius the results are spread out and the map stays zoomed out.zoom:maxkeeps it from zooming in to a single building. - Want this without building it yourself? The locator tag is a ready-made store locator with the form, the list and the map kept together.
- Without a search the page shows nothing; add
default:show_all="yes"withdefault:location,default:radiusanddefault:channelto show the stores around a default location first. - The variables are HTML-escaped already, so they are safe in the HTML and in the
tooltip:contentparameter. - It follows the accessibility checklist: a label for every field, a heading with the number of results, paging links that go to the results (
#results), a map with a name and a skip link, and Directions that says which store and that it opens a new tab. {title},{url_title}and the address come from the search index, a nestedchannel:entriestag is only needed for your custom fields.- The paging links keep the search in the query string (
/stores/P10?location=utrecht&radius=25). filters="services"lets visitors filter on theservicesfield: a store needs one of the checked services. Only the fields infiltersare read from the URL. Addfilter:services="parking"instead for a fixed filter that visitors can't change.- The checkboxes of the store types and services are not checked again after a search; the searched categories are in
{search_category}{search_category:id}{/search_category}if you want to mark them with JavaScript.
{exp:reinos_maps:html_header}
{!-- the search form, it sends the search to this page --}
{exp:reinos_maps_locator:search_form
method="get"
result_url="stores"
location_field="location"
radius_field_max="radius"
search_category_field="type"
form_class="store-search"
}
<label for="store-location">City, postal code or address</label>
<input type="text" id="store-location" name="location" value="{location}" placeholder="e.g. Utrecht">
{!-- shown by the script below, only on a browser that can get the location --}
<button type="button" id="store-my-location" hidden>Use my location</button>
<label for="store-radius">Within</label>
<select id="store-radius" name="radius">
<option value="10" {if radius == 10}selected{/if}>10 km</option>
<option value="25" {if radius == 25 OR radius == ''}selected{/if}>25 km</option>
<option value="50" {if radius == 50}selected{/if}>50 km</option>
<option value="100" {if radius == 100}selected{/if}>100 km</option>
</select>
<fieldset>
<legend>Store type</legend>
{exp:channel:categories category_group="3" style="linear"}
<label>
<input type="checkbox" name="type[]" value="{category_url_title}">
{category_name}
</label>
{/exp:channel:categories}
</fieldset>
{!-- a filter on the checkboxes field "services", allowed by filters="services" on the search_result tag --}
<fieldset>
<legend>Services</legend>
<label><input type="checkbox" name="filter[services][]" value="wifi"> Wifi</label>
<label><input type="checkbox" name="filter[services][]" value="parking"> Parking</label>
<label><input type="checkbox" name="filter[services][]" value="wheelchair"> Wheelchair access</label>
</fieldset>
<button type="submit">Find a store</button>
{/exp:reinos_maps_locator:search_form}
<script>
// fill in the location of the device and search (the browser asks the visitor first)
var locate = document.getElementById('store-my-location');
if (navigator.geolocation && window.isSecureContext) {
locate.hidden = false;
locate.addEventListener('click', function () {
var form = this.form;
navigator.geolocation.getCurrentPosition(function (position) {
form.elements['location'].value = position.coords.latitude.toFixed(6) + ',' + position.coords.longitude.toFixed(6);
form.submit();
}, function () {
alert('We could not get your location. Enter a place instead.');
});
});
}
</script>
{!-- the results: a map and a list, the search is read from the query string --}
<div class="store-locator">
{exp:reinos_maps:map fit_map:markers="yes" zoom:max="14" marker_cluster:enabled="yes" width="100%" height="500px" aria:label="Map of the stores"}
{!-- ExpressionEngine gives the first no_results in a tag pair to that tag: this empty one is for the map tag,
so the no_results below stays with the search_result tag --}
{if no_results}{/if}
<div class="store-results">
{exp:reinos_maps_locator:search_result
channel="stores"
location_field="location"
radius_field_max="radius"
search_category_field="type"
category_match="any"
filters="services"
radius="25"
max_radius="100"
nearest="5"
limit="10"
}
{if no_results}
<h2 id="results" tabindex="-1">No stores found, try another location.</h2>
{/if}
{!-- a heading with the number of results, the paging links go to it --}
{if current_count == 1}
<h2 id="results" tabindex="-1">
{if nearest}No stores within {post_radius} km, these are the nearest{if:elseif abs_total_count == 1}1 store within {post_radius} km{if:else}{abs_total_count} stores within {post_radius} km{/if}
</h2>
<ol class="store-list">
{/if}
<li class="store">
<h3 class="store__title"><a href="{path='stores/detail/{url_title}'}">{title}</a></h3>
<p class="store__distance">{distance} {distance_unit}</p>
<address>
{street_name} {street_number}<br>
{postal_code} {city}
</address>
<a href="https://www.google.com/maps/dir/?api=1&destination={lat},{lng}" target="_blank" rel="noopener">
Directions<span class="visually-hidden"> to {title} (opens in a new tab)</span>
</a>
{maps:add_marker latlng="{lat},{lng}" tooltip:content="{title}"}
</li>
{if current_count == total_count}
</ol>
{/if}
{paginate}
<nav class="store-paging" aria-label="Pages of the results">
{pagination_links}
{previous_page}<a href="{pagination_url}#results">Previous</a>{/previous_page}
{page}<a href="{pagination_url}#results" {if current_page}aria-current="page"{/if}>{pagination_page_number}</a>{/page}
{next_page}<a href="{pagination_url}#results">Next</a>{/next_page}
{/pagination_links}
</nav>
{/paginate}
{/exp:reinos_maps_locator:search_result}
</div>
{!-- the searched location on the map, so the map also fits around it --}
{exp:reinos_maps_locator:current_search_location location_field="location"}
{maps:add_marker latlng="{lat},{lng}" tooltip:content="Your search"}
{/exp:reinos_maps_locator:current_search_location}
{/exp:reinos_maps:map}
</div>
{exp:reinos_maps:html_footer}/* the map (2/3) and the list (1/3) next to each other on a large screen, below each other on a small screen */
.store-locator { display: grid; gap: 20px; }
@media (min-width: 1024px) {
.store-locator { grid-template-columns: 2fr 1fr; align-items: start; }
}
/* the map tag also outputs the target of its skip link next to the map: keep it out of the grid */
.store-locator > .maps-skip-target { position: absolute; }
.store-locator > .maps-skip-link:focus { grid-column: 1 / -1; }
.store-list { margin: 0; padding: 0; list-style: none; }
/* a text only for screen readers */
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }Build it in your own site
What this example needs in ExpressionEngine, step by step.
- Install Maps Locator and Maps.
- Create the category group Store types with the categories Bike shop, E-bike specialist, Repair, Rental. The code uses its ID (
category_group="3"): change it to the ID of your group. - Create the fields, with these short names:
Short name Type Notes store_locationMaps Set the field setting Mode to Single location: one marker per store, placed with the search field. Maps Locator indexes that marker, so this one field is the location for the map and the search. servicesCheckboxes Options: wifi,parking,wheelchair. - Create the channel
stores(Stores) and assign the fields and the category group Store types. - In the settings of Maps Locator, turn on Reverse geocoding: a marker of a Maps field is saved as a coordinate, the reverse geocoding looks up its address and city (the city pages and a search on a city need them).
- Put the code in the template
stores/index: the code links to it. - Add a few stores (the demo site has 34 bike stores in the Netherlands).
- Open
/stores.
This example comes from the Maps Locator docs: the preview runs exactly the code above.