Adress-Autofill im Formular
Adress-Autofill im Formular
Autofill ist für Bestell- und Anmeldeformulare gedacht: ein Textfeld, eine Vorschlagsliste, und die übrigen Felder (Hausnummer, PLZ, Ort) werden aus dem gewählten Vorschlag gefüllt. Jeder Vorschlag bringt seine Koordinaten schon mit.
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; }
});Vollständige HTML-Datei
Speichern und im Browser öffnen. Es braucht nur MapLibre GL JS 4.7.1 und pmtiles 3.2.1 per CDN, dazu den öffentlichen Token (YOUR_TOKEN ersetzen), keinen Build.
<!doctype html>
<html lang="de">
<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="Straße und Nummer, z. B. Herrengasse 13" autocomplete="off" aria-label="Straße"><input id="housenumber" type="search" placeholder="Nr." aria-label="Hausnummer" style="min-width:5rem;flex:0 0 6rem"><input id="postcode" type="search" placeholder="PLZ" aria-label="PLZ" style="min-width:6rem;flex:0 0 7rem"><input id="place" type="search" placeholder="Ort" aria-label="Ort"><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>Verwendete APIs
- Adressen
GET api.mapx.at/v1/autofill/suggest