Maplace.js

Circles and polygons

Circles

Circles in their own colours, with popups; resize the third and see its radius.

Markdown

Map and code

Run the examples on

Radius: 8.0 km

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>
<!-- Circles in their own colours, with popups, the centre selected at start; drag the third
     circle's edge and its radius shows below the map. -->
<maplace-map id="circles-maplibre-map" list selected-id="center">
  <maplace-location location-id="center" type="circle" lat="41.9028" lng="12.4964" radius="5000" title="Within 5 km"
    shape-style='{"stroke":"#1d4ed8","fill":"#3b82f6"}' selected-style='{"stroke":"#c2410c","fill":"#f97316"}'>
    <template><p>The historic centre, on foot.</p></template>
  </maplace-location>
  <maplace-location location-id="ring" type="circle" lat="41.9028" lng="12.4964" radius="15000" title="Within 15 km"
    shape-style='{"stroke":"#15803d","fill":"#22c55e"}' selected-style='{"stroke":"#c2410c","fill":"#f97316"}'>
    <template><p>Inside the ring road, by bike.</p></template>
  </maplace-location>
  <maplace-location location-id="resizable" type="circle" lat="41.8" lng="12.75" radius="8000" title="Change my size"
    editable shape-style='{"stroke":"#7c3aed","fill":"#a78bfa"}'></maplace-location>
</maplace-map>
<p><output id="circles-maplibre-radius">Radius: 8.0 km</output></p>
<script>
  function showCirclesMapLibreRadius(id, radius) {
    if (id !== 'resizable') return;
    document.getElementById('circles-maplibre-radius').textContent = `Radius: ${(radius / 1000).toFixed(1)} km`;
  }
  const circlesMapLibreMap = document.getElementById('circles-maplibre-map');
  circlesMapLibreMap.addEventListener('maplace-locationedit', (event) => {
    showCirclesMapLibreRadius(event.detail.location.id, event.detail.geometry.radius);
  });
  circlesMapLibreMap.addEventListener('maplace-locationchange', (event) => {
    showCirclesMapLibreRadius(event.detail.location.id, event.detail.location.radius);
  });
</script>

How it works

Areas by radius in metres, each in its own colours, the first two with a popup, the centre selected at the start; the third resizes, its radius shown below the map as it changes.

  • HTML: type="circle", radius, shape-style, selected-style and editable on <maplace-location>, a <template> inside each of the first two for its popup (Location attributes), selected-id on the map, and maplace-locationedit and maplace-locationchange listeners (Events).
  • Script and Bundler: type: "circle", radius, style, selectedStyle, content and editable on a location (Circles, Styles), and locationEdit while dragging and locationChange once dropped (Events).
  • React: the same circles, their text in data for popupContent on <MaplaceMap>, with onLocationEdit and onLocationChange on <MaplaceProvider> (Event props).