Basiskarte wechseln
Basiskarte wechseln
Drei fertige Stile teilen sich Position und Zoom. map.setStyle() tauscht die Basis, die Ansicht bleibt.
Code
// Switch the basemap: map, aerial imagery and terrain are three ready-made styles.
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 });
maplibregl.addProtocol('pmtiles', new pmtiles.Protocol().tile);
const STILE = {
streets: 'https://api.mapx.at/v1/styles/streets',
satellite: 'https://api.mapx.at/v1/styles/satellite',
outdoors: 'https://api.mapx.at/v1/styles/outdoors',
};
const map = new maplibregl.Map({ transformRequest, container: 'map', style: STILE.streets, center: [11.4, 47.27], zoom: 11 });
map.addControl(new maplibregl.NavigationControl());
for (const knopf of document.querySelectorAll('[data-stil]')) {
knopf.addEventListener('click', () => {
map.setStyle(STILE[knopf.dataset.stil]); // position and zoom are kept
for (const k of document.querySelectorAll('[data-stil]')) k.setAttribute('aria-pressed', String(k === knopf));
});
}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>
<button type="button" data-stil="streets" aria-pressed="true">Karte</button><button type="button" data-stil="satellite" aria-pressed="false">Luftbild</button><button type="button" data-stil="outdoors" aria-pressed="false">Gelände</button>
<div id="map"></div>
<script>
// Switch the basemap: map, aerial imagery and terrain are three ready-made styles.
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 });
maplibregl.addProtocol('pmtiles', new pmtiles.Protocol().tile);
const STILE = {
streets: 'https://api.mapx.at/v1/styles/streets',
satellite: 'https://api.mapx.at/v1/styles/satellite',
outdoors: 'https://api.mapx.at/v1/styles/outdoors',
};
const map = new maplibregl.Map({ transformRequest, container: 'map', style: STILE.streets, center: [11.4, 47.27], zoom: 11 });
map.addControl(new maplibregl.NavigationControl());
for (const knopf of document.querySelectorAll('[data-stil]')) {
knopf.addEventListener('click', () => {
map.setStyle(STILE[knopf.dataset.stil]); // position and zoom are kept
for (const k of document.querySelectorAll('[data-stil]')) k.setAttribute('aria-pressed', String(k === knopf));
});
}
</script>
</body>
</html>Verwendete APIs
- Styles
GET api.mapx.at/v1/styles/{streets|satellite|outdoors}