Leaflet
A complete Leaflet map to copy and paste.
Livehttps://api.offendersearch.appSandboxhttps://sandbox.offendersearch.appWhat this page builds
A complete map with Leaflet: one HTML page and one script.
- Loads the view when the map first appears, and again each time it stops moving.
- Waits a quarter of a second after the last move, cancels the request it no longer needs, and sends
If-None-Matchso an unchanged view is free. - Draws clusters with their counts. A click on a cluster fits the map to its
bboxat itsexpansionZoom. - Draws pins from zoom 13, and name labels from zoom 15. It reads
modeandlabelfrom the response, so it holds no zoom numbers of its own. - Opens a list from the groups route for a pin that stands for several people, and for an approximate-area pin.
- Opens a record panel from the records route when a pin or a list row is clicked.
- Shows the
noticesentence on the map.
The sample runs in test mode: the request carries
sandbox=true, so it works before the plan is on and is never billed. To show real data, remove the line marked TEST MODE in map.js.Run it
- Save the two files below as
index.htmlandmap.jsin one folder. - Create a map browser key in the dashboard and add
http://localhost:8000to its allowed sites. Put the key inKEYat the top ofmap.js. See Access & keys. - Serve the folder:
python3 -m http.server 8000. Openhttp://localhost:8000. - Zoom in on the clusters. Pins appear, then names. Click a pin with a number on it for the list, and a pin for the record.
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Map — Leaflet</title>
<link rel="stylesheet"
href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css">
<style>
html, body { margin: 0; height: 100%; }
body { font: 14px/1.4 system-ui, sans-serif; }
#map { position: absolute; inset: 0 320px 0 0; }
#panel {
position: absolute; top: 0; right: 0; bottom: 0; width: 320px;
box-sizing: border-box; padding: 16px; overflow: auto;
border-left: 1px solid #d0d5dd; background: #fff;
}
#notice {
position: absolute; left: 12px; bottom: 32px; z-index: 1000;
margin: 0; padding: 6px 10px; border-radius: 6px;
background: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .3);
}
.dot {
display: grid; place-items: center; box-sizing: border-box;map.js
// ── Settings ───────────────────────────────────────────────
// Calling from the browser: a map browser key (os_map_pk_…)
// whose allowed sites include this page.
// Calling through your own server: set API to '' and KEY to ''.
const API = 'https://api.offendersearch.app';
const KEY = 'os_map_pk_your_key';
// ── Leaflet ────────────────────────────────────────────────
// Test mode has people around this point.
const map = L.map('map').setView([39.9612, -82.9988], 10);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© OpenStreetMap contributors',
}).addTo(map);
const layer = L.layerGroup().addTo(map);
function currentView() {
const b = map.getBounds();
return {
west: b.getWest(), south: b.getSouth(),
east: b.getEast(), north: b.getNorth(),
zoom: map.getZoom(),
};
}How it works
The first part of map.js is the only part that knows about Leaflet. Everything from “Load” down is plain JavaScript and is the same in all three library examples.
| Function | What it does with Leaflet |
|---|---|
currentView() | Reads the box from map.getBounds() and the zoom from map.getZoom(). |
place() | Adds an L.marker with an L.divIcon that holds the element, and listens for its click. |
clearMarkers() | Clears the layer group every marker is added to. |
zoomTo() | map.setView() to a centre and a zoom. |
| Start | moveend calls viewChanged(), and it is called once to load the first view. |
| Function | What it does |
|---|---|
viewChanged() | Called on every move end. Restarts a short timer so a drag or a zoom makes one request, not twenty. |
loadView() | Cancels the previous request, sends the box and the zoom, and remembers each answer with its ETag. A 304 redraws the remembered answer. |
draw(view) | Clears the map, then draws clusters, markers and areas, the count line and the notice. |
drawCluster(c) | A circle with count. On click it moves to the middle of bbox at expansionZoom, which loads the next view. |
drawPin(m) | A pin. With a groupId it is a many-people pin and opens the list. With a label it shows the name. |
drawArea(a) | A dashed circle for people known only to a ZIP, city or county. It opens the list. |
openGroup() | One page of the groups route. “Show more” loads the next page. |
openRecord() | The records route. Fills the panel with the name, address, offense, notice and photo. |
Going live
- Turn the Map API on. See Turn it on.
- Remove the
sandboxline markedTEST MODE. - Use a browser key whose allowed sites include your site, or point
APIat your own server and clearKEY. See Access & keys. - The sample uses OpenStreetMap’s public tiles, which are for light use. Use your own tile provider in production.
- A click on a pin opens the record at once, which is one record open. If you would rather show a small popup first, use the
label,housingStatusandhasPhotoalready on a named pin, and call the records route from a “View record” button.