Skip to content

Commit 9e8bd6d

Browse files
committed
Added time slider
1 parent 999da9e commit 9e8bd6d

6 files changed

Lines changed: 104 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ OpenHistoricalMap Americana is a fork of OpenStreetMap Americana, a community pr
2929

3030
## How to use
3131

32-
You can install the OpenHistoricalMap Americana package and [deploy it anywhere](CONTRIBUTING.md#Production%20builds) as a static webpage. For your convenience, we’ve deployed it [on GitHub Pages](https://americanamap.org/). Click the Legend button to learn the meaning of each symbol, line, and color based on the features currently visible on the map. Click the 🌐 button to simulate a physical globe. Click the ⛰️ button to simulate a relief map.
32+
You can install the OpenHistoricalMap Americana package and [deploy it anywhere](CONTRIBUTING.md#Production%20builds) as a static webpage. For your convenience, we’ve deployed it [on GitHub Pages](https://americanamap.org/). Click the Legend button to learn the meaning of each symbol, line, and color based on the features currently visible on the map. Click the 🌐 button to simulate a physical globe. Click the ⛰️ button to simulate a relief map. Adjust the time slider to travel back in time.
3333

3434
The style tries to label places in [your browser’s preferred language](https://www.w3.org/International/questions/qa-lang-priorities). To change this preference, consult your browser’s documentation: [Chrome](https://support.google.com/chrome/answer/173424), [Firefox](https://support.mozilla.org/en-US/kb/use-firefox-another-language), [Safari for macOS](https://support.apple.com/guide/mac-help/change-the-system-language-mh26684/mac), [Safari for iOS](https://support.apple.com/en-us/HT204031). You can also override this preference by adding `&language=` to the URL, followed by a comma-separated list of [IETF language tags](https://www.w3.org/International/articles/language-tags/). For example, here’s a map labeled [in Portuguese, falling back to Spanish](https://americanamap.org/#language=pt,es). If we don’t have the name of a place in any of your preferred languages, the style shows the name in the local language as a last resort.
3535

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@
4040
"dependencies": {
4141
"@americana/diplomat": "^0.3.0",
4242
"@americana/maplibre-shield-generator": "*",
43+
"@openhistoricalmap/maplibre-gl-dates": "^1.3.0",
4344
"color-rgba": "^2.4.0",
4445
"events": "^3.3.0",
4546
"fonteditor-core": "^2.1.11",
4647
"github-fork-ribbon-css": "^0.2.3",
4748
"openmapsamples": "github:adamfranco/OpenMapSamples",
4849
"openmapsamples-maplibre": "github:adamfranco/OpenMapSamples-MapLibre",
50+
"timescope": "^0.0.0-alpha.11",
4951
"tokenfield": "^1.5.2"
5052
},
5153
"devDependencies": {

src/americana.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import config from "./config.js";
44

55
import { LanguageControl } from "./js/language_control.js";
6+
import { TimeControl } from "./js/time_control.js";
67

78
import * as maplibregl from "maplibre-gl";
89
import "maplibre-gl/dist/maplibre-gl.css";
@@ -71,6 +72,7 @@ function shieldDefLoad(shields) {
7172
map.addControl(new maplibregl.NavigationControl(), "top-left");
7273
map.addControl(new maplibregl.GlobeControl(), "top-left");
7374
map.addControl(new HillshadeControl(), "top-left");
75+
map.addControl(new TimeControl(), "bottom-right");
7476

7577
window.addEventListener("languagechange", (event) => {
7678
map.localize();
@@ -112,6 +114,12 @@ function hashChanged(oldURL, newURL) {
112114
if (oldParams.has("terrain") !== newParams.has("terrain")) {
113115
map.shadesHills = newParams.has("terrain");
114116
}
117+
118+
if ((oldParams.get("date") || null) !== (newParams.get("date") || null)) {
119+
map.date = new Date(newParams.get("date"));
120+
} else if (!map.date) {
121+
map.date = new Date();
122+
}
115123
}
116124

117125
let attributionConfig = {

src/js/map_view.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getLocales, localizeStyle } from "@americana/diplomat";
2+
import { filterByDate } from "@openhistoricalmap/maplibre-gl-dates";
23
import maplibregl from "maplibre-gl";
34
import { hillshading } from "../layer/hillshade.js";
45

@@ -30,4 +31,17 @@ export class MapView extends maplibregl.Map {
3031
this.fire("americana.terrain");
3132
});
3233
}
34+
35+
get date(): Date | null {
36+
return this.getGlobalState().date;
37+
}
38+
39+
set date(newValue: Date) {
40+
console.log(`Setting date to ${newValue}`);
41+
this.setGlobalStateProperty("date", newValue);
42+
Promise.resolve(this.style.loaded() || this.once("styledata")).then(() => {
43+
filterByDate(this, newValue);
44+
this.fire("americana.datechanged");
45+
});
46+
}
3347
}

src/js/time_control.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Timescope } from "timescope";
2+
3+
export class TimeControl {
4+
_update() {
5+
// this._timescope.setTime(this._map.date);
6+
}
7+
8+
_onTimeChanged(event) {
9+
const time = event.value;
10+
if (time) {
11+
const date = new Date(time.mul(1000).number());
12+
this._map.date = date;
13+
}
14+
}
15+
16+
onAdd(map) {
17+
this._map = map;
18+
19+
this._container = document.createElement("div");
20+
this._container.className = "maplibregl-ctrl maplibregl-ctrl-group";
21+
this._container.id = "maplibregl-ctrl-time";
22+
23+
Promise.resolve(this._map.loaded() || this._map.once("load")).then(() => {
24+
this._timescope = new Timescope({
25+
target: "#maplibregl-ctrl-time",
26+
style: {
27+
width: "50vw",
28+
},
29+
timeRange: [new Date("1800-01-01"), null],
30+
time: null,
31+
zoom: -20,
32+
tracks: {
33+
timeAxis: {
34+
timeFormat: ({ time }) =>
35+
new Date(time.mul(1000).number()).toLocaleDateString(),
36+
},
37+
},
38+
});
39+
this._timescope.on("timechanged", (event) => this._onTimeChanged(event));
40+
});
41+
this._map.on("americana.datechanged", () => this._update());
42+
43+
return this._container;
44+
}
45+
46+
onRemove() {
47+
this._container.remove();
48+
this._timescope.dispose();
49+
this._map.off("americana.datechanged");
50+
this._map = undefined;
51+
}
52+
}

0 commit comments

Comments
 (0)