Change between light and dark mode based on the time of day
This example demonstrates the process of altering the map display from a light to a dark mode, depending on the current time of day.
To achieve this, the MapTiler Geolocation API is utilized to acquire the user’s location data, while the SunCalc library is employed to compute the sunrise and sunset timings based on the user’s location.
NPM module setup
npm install --save @maptiler/sdk suncalc bootstrap bootstrap-icons
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';
import * as SunCalc from 'suncalc';
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap-icons/font/bootstrap-icons.css';
config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
let map;
let activeMode = "light";
const now = new Date();
let times;
function getStyleByMode(mode) {
return (mode == 'dark') ? MapStyle.BASE.DARK : MapStyle.BASE.LIGHT;
}
function switchMode(mode) {
activeMode = mode;
const style = getStyleByMode(mode);
map.setStyle(style);
}
function timeFormat(time) {
return time.toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
hour12: false,
});
}
function dateFromHours(hours) {
const dateHours = new Date();
const [h, m] = hours.split(":");
dateHours.setHours(h, m);
return dateHours;
}
function isDay(date) {
return (date > times.sunriseEnd && date < times.sunset) ? true : false;
}
//call IP geolocation API see https://docs.maptiler.com/cloud/api/geolocation/
maptilersdk.geolocation.info()
.then((data) => {
const userLngLat = [data.longitude, data.latitude];
times = SunCalc.getTimes(now, data.latitude, data.longitude);
document.getElementById('sun').innerText = timeFormat(times.sunriseEnd);
document.getElementById('moon').innerText = timeFormat(times.sunset);
document.getElementById('location').innerText = ` ${data.city}, ${data.country}`;
document.getElementById('appt').value = timeFormat(now).substring(0,5);
activeMode = isDay(now) ? 'light' : 'dark';
map = map = new Map({
container: 'map',
style: getStyleByMode(activeMode),
center: userLngLat,
zoom: 12,
hash: true
});
});
document.getElementById("appt").addEventListener('change', function() {
const hours = dateFromHours(this.value);
if (isDay(hours) && activeMode == "dark") {
switchMode("light");
} else if (!isDay(hours) && activeMode == "light") {
switchMode("dark");
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex">
<title>How to change the style (light/dark) of the map based on the time of day</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="map">
<div class="btns hero-card rounded-3 shadow-lg">
<form>
<div>
<label for="appt" class="form-label">Select a time:</label>
<input type="time" class="form-control" id="appt" name="appt">
</div>
<div class="mt-1">
<div><i class="bi bi-geo-alt"></i></i><span id="location"></span></div>
</div>
<div class="hours mt-1">
<div><i class="bi bi-sun"></i><span id="sun"></span></div>
<div><i class="bi bi-moon"></i><span id="moon"></span></div>
</div>
</form>
</div>
</div>
<script type="module" src="./main.js"></script>
</body>
</html>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
.btns {
z-index: 1000;
position: relative;
background-color: var(--bs-white);
width: fit-content;
padding: 10px;
}
.hours{
display: flex;
justify-content: space-around;
}
Related examples
Set dark mode based on system settings
ExamplesSwitch map light/dark mode based on system color settings.