Style Control
@maptoolkit/maplibre-style-control
shows the available styles as thumbnails and swaps the map style when one is picked. Its
built-in styles are the seven community styles, so it needs no configuration and
no API key.
Install
npm install @maptoolkit/maplibre-style-control maplibre-glUsage
import * as maplibregl from "maplibre-gl";
import { StyleControl } from "@maptoolkit/maplibre-style-control";
import "@maptoolkit/maplibre-style-control/style.css";
const map = new maplibregl.Map({
container: "map",
style: "https://styles.maptoolkit.org/summer.json",
center: [16.3738, 48.2082],
zoom: 12,
});
map.addControl(new StyleControl(), "top-left");Give the control a position. It defaults to bottom-left, where the
Maptoolkit logo sits, and a panel opened in the same corner
covers the logo. Attribution that is covered does not count as displayed.
Switching the style removes every source and layer on the map, including the ones you added
yourself. Add them again on the next styledata event.
Without a bundler
The package is an ES module only. Load it from a CDN and map maplibre-gl in an
import map:
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maplibre-gl@6.16.0/dist/maplibre-gl.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-style-control@1.1.2/dist/maplibre-style-control.css" />
<script type="importmap">
{
"imports": {
"maplibre-gl": "https://cdn.jsdelivr.net/npm/maplibre-gl@6.16.0/dist/maplibre-gl.mjs"
}
}
</script>
<script type="module">
import * as maplibregl from "maplibre-gl";
import { StyleControl } from "https://cdn.jsdelivr.net/npm/@maptoolkit/maplibre-style-control@1.1.2/dist/maplibre-style-control.js";
const map = new maplibregl.Map({
container: "map",
style: "https://styles.maptoolkit.org/summer.json",
center: [16.3738, 48.2082],
zoom: 12,
});
map.addControl(new StyleControl(), "top-left");
</script>Options, events and styling
The styles option replaces the built-in list with styles of your own, and active sets the
style that is selected first. All options, the style.set event, the methods, the
translations and the CSS custom properties are described in the
Style Control reference on
docs.maptoolkit.com.
License
The Style Control is open source under the BSD 3-Clause License.