JSFiddle - React, Tailwind, and code Playground
by Tristen Brown
HTML
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-assembly/mbx/v1.2.0/assembly.min.css">
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v3.0.0-beta.1/mapbox-gl.css">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.0.0-beta.1/mapbox-gl.js"></script>
<div id="map" class="absolute top bottom left right w-full"></div>
<div id="toggle-globe" class="btn btn--s mx12 my12 relative z1 cursor-pointer">Toggle globe</div>
JavaScript
mapboxgl.accessToken = 'pk.eyJ1IjoidHJpc3RlbiIsImEiOiJjajZoOXU4Z2kwNnppMnlxd2d6bTFvZ2xtIn0.PP7AoUakMfeqdXFHdsY0oA';
let globe = false;
const style = {
"version": 8,
"center": [-68, 51],
"zoom": 3,
"sources": {
"composite": {
"url": "mapbox://mapbox.mapbox-streets-v8,mapbox.country-boundaries-v1",
"type": "vector"
}
},
"glyphs": "mapbox://fonts/mapbox/{fontstack}/{range}.pbf",
"layers": [{
"id": "labels",
"type": "symbol",
"source": "composite",
"source-layer": "place_label",
"layout": {
"text-field": ["get", "name"]
},
"paint": {
"text-color": "red"
}
},
{
"id": "countries",
"type": "fill",
"source": "composite",
"source-layer": "country_boundaries"
}
]
};
const map = new mapboxgl.Map({
container: 'map',
style: style
});
document.getElementById('toggle-globe').addEventListener('click', () => {
if (globe) {
map.setStyle(style);
globe = false;
} else {
map.setStyle({ ...style, projection: { name: 'globe' }});
globe = true;
}
});