@mapbox-controls/styles
v6.0.0
Published
Change map style
Readme
💅 @mapbox-controls/styles
Control to change map style among provided.
npm i @mapbox-controls/stylesimport StylesControl from "@mapbox-controls/styles";
import "@mapbox-controls/styles/src/index.css";
const styles = [
{
label: "Streets",
style: "mapbox://styles/mapbox/streets-v12",
},
{
label: "Satellite",
style: "mapbox://styles/mapbox/satellite-streets-v12",
},
];
map.addControl(new StylesControl({ styles }), "top-left");
// or with compact view
map.addControl(new StylesControl({ styles, compact: true }), "top-left");style is a style URL or a style object. The control selects the current map style: a URL by the URL, an object by its name.
const osm = {
version: 8,
name: "OpenStreetMap",
sources: {
osm: {
type: "raster",
tiles: ["https://tile.openstreetmap.org/{z}/{x}/{y}.png"],
tileSize: 256,
attribution: "© OpenStreetMap contributors",
},
},
layers: [{ id: "osm", type: "raster", source: "osm" }],
};
map.addControl(new StylesControl({ styles: [...styles, { label: "OSM", style: osm }] }));Switching rebuilds the whole style, so sources and layers added at runtime are removed: use style.load event to redraw them.
Options
export type Style = {
label: string;
style: string | (StyleSpecification & { name: string });
};
export type ControlOptions = {
styles: Style[];
onChange?: (style: Style) => void;
compact?: boolean;
};