A Leaflet control for adding an attractive basemap bar.
https://github.com/GreenInfo-Network/Leaflet-Control-BasemapBar
http://greeninfo-network.github.io/Leaflet-Control-BasemapBar/
Include the CSS and JS files using tags as usual:
<script type="text/javascript" src="leaflet-control-basemapbar.js"></script>
<link rel="stylesheet" type="text/css" href="leaflet-control-basemapbar.css" />
Then use it:
const basemap_offerings = [
{
id: 'Topo',
tooltip: 'ESRI Topographic Basemap'
layer: L.tileLayer('https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}.jpg', {
attribution:'© <a target="_blank" href="http://esri.com/" target="_blank">ESRI</a>'
})
},
{
id: 'Positron',
tooltip: 'Mapbox Positron'
layer: L.tileLayer('https://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png', {
attribution: 'Map tiles by <a target="_blank" href="http://www.mapbox.com">MapBox</a>.<br />Data © <a target="_blank" href="http://openstreetmap.org/copyright" target="_blank">OpenStreetMap contributings</a>',
}),
},
{
id: 'OSM',
tooltip: 'Open Street Map',
layer: L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution:'© <a target="_blank" href="http://openstreetmap.org/copyright" target="_blank">OpenStreetMap contributors</a>'
}}
},
];
const mymap = L.map();
mymap.layerpicker = new L.Control.BasemapBar({
layers: basemap_offerings,
}).addTo(mymap);
layers= A list of layers to offer, and their titles. =id= the label to display in the bar; this must be unique, as this is used byselectOption()to trigger a selection programmatically =tooltip= a tooltip title when mouse hovers over this choice =layer= a L.TileLayer instance, or probably almost any type of L.Layer as long as you set itspaneappropriatelyposition= The usual L.Control positioning for the control; defaults to 'topleft'expanded= true to have the options expanded by default when the control is added; defaults to false (collapsed)expandButtonHTML= set the HTML content of the button when the bar is collapsedcollapseButtonHTML= set the HTML content of the collapse button when the bar is expandedenhancedfocus= adds an enhanced:focusoutline on the control's buttons to aid people with low vision; particularly useful when using basemaps with a dark color
expand()= expand the interfacecollapse()= collapse the interfacewhichLayer()= get theidof the currently-selected layerselectLayer(layerid)= select a layer by itsid