Created
March 31, 2018 07:05
-
-
Save apetov/226f9c32b0eab1f17a5f4ac167f5f9c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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 = '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="https://carto.com/attributions">CARTO</a>', | |
nightAttr = '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> © <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