Skip to content

Commit 9ff5697

Browse files
committed
Remember route endpoints. #11
1 parent 8655781 commit 9ff5697

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

web/src/RouteMode.svelte

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
<script lang="ts">
2-
import { LngLat } from "maplibre-gl";
32
import { onDestroy, onMount } from "svelte";
43
import { GeoJSON, LineLayer, Marker } from "svelte-maplibre";
54
import { constructMatchExpression, notNull } from "./common";
65
import ModalFilterLayer from "./ModalFilterLayer.svelte";
76
import RenderNeighbourhood from "./RenderNeighbourhood.svelte";
87
import SplitComponent from "./SplitComponent.svelte";
9-
import { app, map, mode } from "./stores";
8+
import { app, map, mode, route_pt_a, route_pt_b } from "./stores";
109
1110
export let prevMode: "network" | "neighbourhood";
1211
13-
let pt_a: LngLat = randomPoint();
14-
let pt_b: LngLat = randomPoint();
15-
1612
$: gj = JSON.parse(
17-
$app!.compareRoute(pt_a.lng, pt_a.lat, pt_b.lng, pt_b.lat),
13+
$app!.compareRoute(
14+
$route_pt_a.lng,
15+
$route_pt_a.lat,
16+
$route_pt_b.lng,
17+
$route_pt_b.lat,
18+
),
1819
);
1920
2021
onMount(() => {
@@ -24,13 +25,6 @@
2425
$map?.keyboard.enable();
2526
});
2627
27-
function randomPoint(): LngLat {
28-
let bounds = $app!.getBounds();
29-
let lng = bounds[0] + Math.random() * (bounds[2] - bounds[0]);
30-
let lat = bounds[1] + Math.random() * (bounds[3] - bounds[1]);
31-
return new LngLat(lng, lat);
32-
}
33-
3428
function onKeyDown(e: KeyboardEvent) {
3529
if (e.key == "Escape") {
3630
e.stopPropagation();
@@ -79,8 +73,12 @@
7973
}}
8074
/>
8175
</GeoJSON>
82-
<Marker bind:lngLat={pt_a} draggable><span class="dot">A</span></Marker>
83-
<Marker bind:lngLat={pt_b} draggable><span class="dot">B</span></Marker>
76+
<Marker bind:lngLat={$route_pt_a} draggable
77+
><span class="dot">A</span></Marker
78+
>
79+
<Marker bind:lngLat={$route_pt_b} draggable
80+
><span class="dot">B</span></Marker
81+
>
8482
</div>
8583
</SplitComponent>
8684

web/src/stores.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { LTN } from "backend";
22
import type { Feature, Polygon } from "geojson";
3-
import type { Map } from "maplibre-gl";
3+
import { LngLat, type Map } from "maplibre-gl";
44
import { writable, type Writable } from "svelte/store";
55
import { RouteTool } from "./common/snapper/route_tool";
66

@@ -33,17 +33,20 @@ export type Mode =
3333
mode: "debug-gj";
3434
};
3535

36-
export let app: Writable<LTN | null> = writable(null);
37-
// A way for different components to know when internal app state has changed
38-
// and they might need to rerender
39-
export let mutationCounter: Writable<number> = writable(1);
40-
export let mode: Writable<Mode> = writable({ mode: "title" });
41-
export let showBasemap: Writable<boolean> = writable(true);
4236
export let map: Writable<Map | null> = writable(null);
43-
export let route_tool: Writable<RouteTool | null> = writable(null);
37+
export let showBasemap: Writable<boolean> = writable(true);
4438

4539
export let example: Writable<string> = writable("");
4640
export let showAbout: Writable<boolean> = writable(true);
4741

4842
export let sidebarContents: Writable<HTMLDivElement | null> = writable(null);
4943
export let mapContents: Writable<HTMLDivElement | null> = writable(null);
44+
45+
export let app: Writable<LTN | null> = writable(null);
46+
export let route_tool: Writable<RouteTool | null> = writable(null);
47+
export let route_pt_a: Writable<LngLat> = writable(new LngLat(0, 0));
48+
export let route_pt_b: Writable<LngLat> = writable(new LngLat(0, 0));
49+
// A way for different components to know when internal app state has changed
50+
// and they might need to rerender
51+
export let mutationCounter: Writable<number> = writable(1);
52+
export let mode: Writable<Mode> = writable({ mode: "title" });

web/src/title/MapLoader.svelte

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
<script lang="ts">
2+
import { LngLat } from "maplibre-gl";
23
import { LTN } from "backend";
34
import { onMount } from "svelte";
45
import { Loading, OverpassSelector } from "../common";
56
import { RouteTool } from "../common/snapper/route_tool";
6-
import { app, example, map, mode, route_tool } from "../stores";
7+
import {
8+
app,
9+
example,
10+
map,
11+
mode,
12+
route_tool,
13+
route_pt_a,
14+
route_pt_b,
15+
} from "../stores";
716
817
let msg: string | null = null;
918
let useLocalVite = false;
@@ -59,6 +68,8 @@
5968
Array.from($app.getBounds()) as [number, number, number, number],
6069
{ animate: false },
6170
);
71+
$route_pt_a = randomPoint();
72+
$route_pt_b = randomPoint();
6273
}
6374
6475
function gotXml(e: CustomEvent<string>) {
@@ -92,6 +103,13 @@
92103
}
93104
msg = null;
94105
}
106+
107+
function randomPoint(): LngLat {
108+
let bounds = $app!.getBounds();
109+
let lng = bounds[0] + Math.random() * (bounds[2] - bounds[0]);
110+
let lat = bounds[1] + Math.random() * (bounds[3] - bounds[1]);
111+
return new LngLat(lng, lat);
112+
}
95113
</script>
96114

97115
<Loading {msg} />

0 commit comments

Comments
 (0)