Skip to content

Commit 1eabe50

Browse files
updated for hiding unrelated stops
1 parent ef6d531 commit 1eabe50

File tree

2 files changed

+76
-12
lines changed

2 files changed

+76
-12
lines changed

src/components/map/GoogleMap.svelte

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
import RouteMap from './RouteMap.svelte';
1717
1818
export let selectedTrip;
19+
export let selectedRoute = null;
20+
export let showRoute = false;
1921
2022
const dispatch = createEventDispatcher();
2123
2224
let map = null;
2325
2426
let markers = [];
27+
let allStops = [];
2528
2629
async function loadStopsForLocation(lat, lng) {
2730
const response = await fetch(`/api/oba/stops-for-location?lat=${lat}&lng=${lng}`);
@@ -53,20 +56,54 @@
5356
async function loadStopsAndAddMarkers(lat, lng) {
5457
const json = await loadStopsForLocation(lat, lng);
5558
const stops = json.data.list;
59+
allStops = [...allStops, ...stops];
5660
57-
for (const s of stops) {
58-
if (markerExists(s)) {
59-
continue;
60-
}
61+
clearAllMarkers();
6162
62-
addMarker(s);
63+
if (showRoute && selectedRoute) {
64+
const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id));
65+
stopsToShow.forEach((s) => addMarker(s));
66+
} else {
67+
stops.forEach((s) => addMarker(s));
6368
}
6469
}
6570
66-
function markerExists(s) {
67-
return markers.some((marker) => marker.s.id === s.id);
71+
function clearAllMarkers() {
72+
markers.forEach(({ marker, overlay, element }) => {
73+
if (marker) {
74+
marker.setMap(null);
75+
}
76+
if (overlay) {
77+
if (overlay.map) {
78+
overlay.map = null;
79+
}
80+
if (overlay.draw) {
81+
overlay.draw = () => {};
82+
}
83+
if (overlay.onRemove) {
84+
overlay.onRemove();
85+
}
86+
}
87+
if (element && element.parentNode) {
88+
element.parentNode.removeChild(element);
89+
}
90+
});
91+
markers = [];
92+
}
93+
94+
$: if (selectedRoute && showRoute) {
95+
clearAllMarkers();
96+
const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id));
97+
stopsToShow.forEach((s) => addMarker(s));
98+
} else if (!showRoute || !selectedRoute) {
99+
clearAllMarkers();
100+
allStops.forEach((s) => addMarker(s));
68101
}
69102
103+
/* function markerExists(s) {
104+
return markers.some((marker) => marker.s.id === s.id);
105+
} */
106+
70107
function addMarker(s) {
71108
const container = document.createElement('div');
72109
document.body.appendChild(container);
@@ -109,6 +146,11 @@
109146
container.style.position = 'absolute';
110147
this.getPanes().overlayMouseTarget.appendChild(container);
111148
};
149+
overlay.onRemove = function () {
150+
if (container.parentNode) {
151+
container.parentNode.removeChild(container);
152+
}
153+
};
112154
markers.push({ s, marker, overlay, element: container });
113155
}
114156
@@ -165,7 +207,7 @@
165207
<div id="map"></div>
166208

167209
{#if selectedTrip}
168-
<RouteMap map={map} tripId={selectedTrip.tripId} />
210+
<RouteMap {map} tripId={selectedTrip.tripId} />
169211
{/if}
170212

171213
<LocationButton on:locationObtained={handleLocationObtained} />

src/routes/+page.svelte

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
66
let stop;
77
let selectedTrip = null;
8+
let showRoute = false;
9+
let selectedRoute = null;
810
911
function stopSelected(event) {
1012
stop = event.detail.stop;
@@ -13,18 +15,38 @@
1315
function closePane() {
1416
stop = null;
1517
selectedTrip = null;
18+
selectedRoute = null;
19+
showRoute = false;
20+
clearAllMarkers();
1621
}
1722
1823
function tripSelected(event) {
19-
selectedTrip = event.detail;
20-
}
24+
selectedTrip = event.detail;
25+
showRoute = true;
26+
selectedRoute = {
27+
id: event.detail.routeId,
28+
shortName: event.detail.routeShortName
29+
};
30+
}
2131
32+
function toggleRoute() {
33+
showRoute = !showRoute;
34+
if (!showRoute) {
35+
selectedRoute = null;
36+
}
37+
}
2238
</script>
2339

2440
{#if stop}
2541
<ModalPane on:close={closePane}>
26-
<StopPane {stop} on:tripSelected={tripSelected} />
42+
<StopPane
43+
{stop}
44+
on:tripSelected={tripSelected}
45+
{showRoute}
46+
{toggleRoute}
47+
selectedRouteId={selectedRoute?.id}
48+
/>
2749
</ModalPane>
2850
{/if}
2951

30-
<GoogleMap {selectedTrip} on:stopSelected={stopSelected} />
52+
<GoogleMap {selectedTrip} {selectedRoute} on:stopSelected={stopSelected} {showRoute} />

0 commit comments

Comments
 (0)