Skip to content

Instantly share code, notes, and snippets.

@apetov
Created March 31, 2018 07:05
Show Gist options
  • Save apetov/226f9c32b0eab1f17a5f4ac167f5f9c0 to your computer and use it in GitHub Desktop.
Save apetov/226f9c32b0eab1f17a5f4ac167f5f9c0 to your computer and use it in GitHub Desktop.
'use strict';
export class MapMode {
constructor() {
this.currentMode = 'day';
}
switchMode(lat, lng) {
const sunCalc = require('suncalc');
let currentTime = new Date();
let times = sunCalc.getTimes(currentTime, lat, lng);
if(currentTime <= times.sunrise || currentTime >= times.sunset) {
this.currentMode = 'night';
} else {
this.currentMode = 'day';
}
}
get mode() {
return this.currentMode;
}
get tileUrl() {
let dayTile = 'http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',
nightTile = 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/dark_all/{z}/{x}/{y}.png';
return (this.currentMode === 'day') ? dayTile : nightTile;
}
get attribution() {
let dayAttr = '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, &copy; <a href="https://carto.com/attributions">CARTO</a>',
nightAttr = '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> &copy; <a href="http://cartodb.com/attributions">CartoDB</a>';
return (this.currentMode === 'day') ? dayAttr : nightAttr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment