Maplace.js

Search and filters

Sort by distance

A button on the map that sorts the list by distance from the visitor.

Markdown

Map and code

Run the examples on

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

Loading the map…

The map couldn't load.

These maps come from Google Maps and OpenFreeMap, which see your IP address. They stay off until you say so, once for the whole site. What loading the maps sends

<!doctype html>
<script src="https://cdn.jsdelivr.net/npm/@maplacejs/maplace@1/dist/maplace-element-maplibre.iife.js"></script>
<!-- Places in a list beside the map; the "My location" button on the map sorts it nearest first, each entry saying how far. -->
<maplace-map id="nearest-maplibre-map" list list-sort="distance" my-location>
  <maplace-location location-id="london" lat="51.5074" lng="-0.1278" title="London"></maplace-location>
  <maplace-location location-id="bristol" lat="51.4545" lng="-2.5879" title="Bristol"></maplace-location>
  <maplace-location location-id="birmingham" lat="52.4862" lng="-1.8904" title="Birmingham"></maplace-location>
  <maplace-location location-id="manchester" lat="53.4808" lng="-2.2426" title="Manchester"></maplace-location>
  <maplace-location location-id="leeds" lat="53.8008" lng="-1.5491" title="Leeds"></maplace-location>
</maplace-map>

How it works

The My location button on the map asks the browser for the visitor’s position only when it is pressed, then sorts the list nearest first, each entry saying how far in a straight line, marks the place with a dot and moves the map there.

  • Note: if the visitor refuses, or the page isn’t on HTTPS, a line under the button says “Location unavailable” and nothing else changes.
  • HTML: list-sort="distance" and my-location on <maplace-map>, with distance-unit="mile" for miles (Map attributes).
  • Script and Bundler: sort: "distance" in list, and myLocation: true in the map’s options (Distances).
  • React: <LocationList sort="distance" />, and <MyLocation /> inside the <MaplaceMap> (<MyLocation>).
  • Another point: setOrigin takes any point, so a click measures from there: map.on("mapClick", ({ position }) => map.setOrigin(position)), and a form of your own measures from a typed address.