-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistance.ts
More file actions
86 lines (70 loc) · 2.59 KB
/
Copy pathdistance.ts
File metadata and controls
86 lines (70 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import maplibregl, { type MapMouseEvent } from "maplibre-gl";
import { getStyle } from "basemapkit";
import { Protocol } from "pmtiles";
import { glyphs, lang, pmtiles, sprite } from "./constant";
import { DistanceTiledLayer, Colormap } from "../lib";
function createDistanceColormap(distanceKm: number): Colormap {
return Colormap.fromColormapDescription([
0,
"rgba(9, 14, 31, 0.0)",
distanceKm,
"rgba(9, 14, 31, 0.0)",
distanceKm * 1.05,
"rgba(9, 14, 31, 0.6)",
Math.max(distanceKm * 2, 10),
"rgba(9, 14, 31, 0.6)",
]);
}
function sliderValueToRadius(sliderVal: string): number {
return Math.max(0.1, Number.parseFloat(sliderVal) ** 2 * 3000);
}
export async function distanceDemo(globe: boolean) {
maplibregl.addProtocol("pmtiles", new Protocol().tile);
const container = document.getElementById("map");
if (!container) throw new Error('There is no div with the id: "map" ');
const seriesSlider = document.getElementById("series-slider") as HTMLInputElement;
if (!seriesSlider) throw new Error("Slider not working");
const opacitySlider = document.getElementById("opacity-slider") as HTMLInputElement;
if (!opacitySlider) throw new Error("Slider not working");
opacitySlider.min = "0.01";
opacitySlider.max = "1";
opacitySlider.step = "0.01";
opacitySlider.value = "0.2";
const pickindDisplay = document.getElementById("picking-display");
if (!pickindDisplay) throw new Error("Picking display not working");
const dateDisplay = document.getElementById("date-display");
if (!dateDisplay) throw new Error("Date display not working");
[seriesSlider, pickindDisplay, dateDisplay].forEach((el) => el.style.setProperty("display", "none"));
const style = getStyle("avenue", {
pmtiles,
sprite,
glyphs,
lang,
hidePOIs: true,
globe,
});
const center = { lng: 27.35, lat: 38.92 };
const map = new maplibregl.Map({
container,
hash: false,
zoom: 4,
center,
style: style,
maxPitch: 89,
});
await new Promise((resolve) => map.on("load", resolve));
const radius = sliderValueToRadius(opacitySlider.value);
const layer = new DistanceTiledLayer("distance-layer", {
referencePosition: center,
colormap: createDistanceColormap(radius),
});
map.addLayer(layer);
opacitySlider.addEventListener("input", () => {
const radius = sliderValueToRadius(opacitySlider.value);
createDistanceColormap(Number.parseFloat(opacitySlider.value));
layer.setColormap(createDistanceColormap(radius));
});
map.on("mousemove", async (e: MapMouseEvent) => {
layer.setRefeferenceLocation(e.lngLat);
});
}