Switch the basemap
Switch the basemap
Three ready-made styles share position and zoom. map.setStyle() swaps the base, the view stays.
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));
});
}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>
<button type="button" data-stil="streets" aria-pressed="true">Map</button><button type="button" data-stil="satellite" aria-pressed="false">Aerial</button><button type="button" data-stil="outdoors" aria-pressed="false">Terrain</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>APIs used
- Styles
GET api.mapx.at/v1/styles/{streets|satellite|outdoors}