Address autofill in a form
Address autofill in a form
Autofill is made for checkout and registration forms: one text field, one suggestion list, and the other fields (house number, postcode, place) are filled from the chosen suggestion. Each suggestion already carries its coordinates.
Code
// Address autofill: type a street and number, pick a suggestion, the form fields fill themselves.
const TOKEN = 'YOUR_TOKEN'; // your public token (mx_pk_…) from https://account.mapx.at
// Adds the token to every request MapLibre makes to mapx.at (style, tiles, fonts, sprites), never to other hosts.
const transformRequest = (url) => ({ url: /^https:\/\/([\w-]+\.)?mapx\.at\//.test(url) ? url + (url.includes('?') ? '&' : '?') + 'access_token=' + TOKEN : url });
const AUTH = { headers: { Authorization: 'Bearer ' + TOKEN } };
maplibregl.addProtocol('pmtiles', new pmtiles.Protocol().tile);
const map = new maplibregl.Map({ transformRequest, container: 'map', style: 'https://api.mapx.at/v1/styles/streets', center: [13.8, 47.7], zoom: 6.5 });
map.addControl(new maplibregl.NavigationControl());
const f = Object.fromEntries(['street', 'housenumber', 'postcode', 'place'].map((k) => [k, document.getElementById(k)]));
const list = document.getElementById('treffer');
let marker;
f.street.addEventListener('input', async () => {
list.replaceChildren();
if (f.street.value.trim().length < 3) return;
try {
const res = await fetch(`https://api.mapx.at/v1/autofill/suggest?q=${encodeURIComponent(f.street.value)}&limit=5&session_token=demo`, AUTH);
if (!res.ok) throw new Error('HTTP ' + res.status);
for (const s of (await res.json()).suggestions) {
const li = document.createElement('li'), b = document.createElement('button');
b.type = 'button'; b.textContent = s.full_address;
b.addEventListener('click', () => {
for (const k of Object.keys(f)) f[k].value = s[k] ?? '';
marker?.remove();
marker = new maplibregl.Marker().setLngLat(s.coordinates).addTo(map);
map.flyTo({ center: s.coordinates, zoom: 16 });
list.replaceChildren();
});
li.append(b); list.append(li);
}
} catch (e) { list.textContent = 'Error: ' + e.message; }
});Complete HTML file
Save and open in a browser. All it needs is MapLibre GL JS 4.7.1 and pmtiles 3.2.1 from a CDN, plus your public token (replace YOUR_TOKEN), no build.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>mapx Beispiel</title>
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@4.7.1/dist/maplibre-gl.css">
<script src="https://unpkg.com/maplibre-gl@4.7.1/dist/maplibre-gl.js"></script>
<script src="https://unpkg.com/pmtiles@3.2.1/dist/pmtiles.js"></script>
<style>html,body{margin:0;font:14px system-ui,sans-serif}#map{height:70vh}</style>
</head>
<body>
<input id="street" type="search" placeholder="Street and number, e.g. Herrengasse 13" autocomplete="off" aria-label="Street"><input id="housenumber" type="search" placeholder="No." aria-label="House number" style="min-width:5rem;flex:0 0 6rem"><input id="postcode" type="search" placeholder="Postcode" aria-label="Postcode" style="min-width:6rem;flex:0 0 7rem"><input id="place" type="search" placeholder="Place" aria-label="Place"><ul id="treffer"></ul>
<div id="map"></div>
<script>
// Address autofill: type a street and number, pick a suggestion, the form fields fill themselves.
const TOKEN = 'YOUR_TOKEN'; // your public token (mx_pk_…) from https://account.mapx.at
// Adds the token to every request MapLibre makes to mapx.at (style, tiles, fonts, sprites), never to other hosts.
const transformRequest = (url) => ({ url: /^https:\/\/([\w-]+\.)?mapx\.at\//.test(url) ? url + (url.includes('?') ? '&' : '?') + 'access_token=' + TOKEN : url });
const AUTH = { headers: { Authorization: 'Bearer ' + TOKEN } };
maplibregl.addProtocol('pmtiles', new pmtiles.Protocol().tile);
const map = new maplibregl.Map({ transformRequest, container: 'map', style: 'https://api.mapx.at/v1/styles/streets', center: [13.8, 47.7], zoom: 6.5 });
map.addControl(new maplibregl.NavigationControl());
const f = Object.fromEntries(['street', 'housenumber', 'postcode', 'place'].map((k) => [k, document.getElementById(k)]));
const list = document.getElementById('treffer');
let marker;
f.street.addEventListener('input', async () => {
list.replaceChildren();
if (f.street.value.trim().length < 3) return;
try {
const res = await fetch(`https://api.mapx.at/v1/autofill/suggest?q=${encodeURIComponent(f.street.value)}&limit=5&session_token=demo`, AUTH);
if (!res.ok) throw new Error('HTTP ' + res.status);
for (const s of (await res.json()).suggestions) {
const li = document.createElement('li'), b = document.createElement('button');
b.type = 'button'; b.textContent = s.full_address;
b.addEventListener('click', () => {
for (const k of Object.keys(f)) f[k].value = s[k] ?? '';
marker?.remove();
marker = new maplibregl.Marker().setLngLat(s.coordinates).addTo(map);
map.flyTo({ center: s.coordinates, zoom: 16 });
list.replaceChildren();
});
li.append(b); list.append(li);
}
} catch (e) { list.textContent = 'Error: ' + e.message; }
});
</script>
</body>
</html>APIs used
- Address
GET api.mapx.at/v1/autofill/suggest