|
16 | 16 | import RouteMap from './RouteMap.svelte';
|
17 | 17 |
|
18 | 18 | export let selectedTrip;
|
| 19 | + export let selectedRoute = null; |
| 20 | + export let showRoute = false; |
19 | 21 |
|
20 | 22 | const dispatch = createEventDispatcher();
|
21 | 23 |
|
22 | 24 | let map = null;
|
23 | 25 |
|
24 | 26 | let markers = [];
|
| 27 | + let allStops = []; |
25 | 28 |
|
26 | 29 | async function loadStopsForLocation(lat, lng) {
|
27 | 30 | const response = await fetch(`/api/oba/stops-for-location?lat=${lat}&lng=${lng}`);
|
|
53 | 56 | async function loadStopsAndAddMarkers(lat, lng) {
|
54 | 57 | const json = await loadStopsForLocation(lat, lng);
|
55 | 58 | const stops = json.data.list;
|
| 59 | + allStops = [...allStops, ...stops]; |
56 | 60 |
|
57 |
| - for (const s of stops) { |
58 |
| - if (markerExists(s)) { |
59 |
| - continue; |
60 |
| - } |
| 61 | + clearAllMarkers(); |
61 | 62 |
|
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)); |
63 | 68 | }
|
64 | 69 | }
|
65 | 70 |
|
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)); |
68 | 101 | }
|
69 | 102 |
|
| 103 | + /* function markerExists(s) { |
| 104 | + return markers.some((marker) => marker.s.id === s.id); |
| 105 | + } */ |
| 106 | +
|
70 | 107 | function addMarker(s) {
|
71 | 108 | const container = document.createElement('div');
|
72 | 109 | document.body.appendChild(container);
|
|
109 | 146 | container.style.position = 'absolute';
|
110 | 147 | this.getPanes().overlayMouseTarget.appendChild(container);
|
111 | 148 | };
|
| 149 | + overlay.onRemove = function () { |
| 150 | + if (container.parentNode) { |
| 151 | + container.parentNode.removeChild(container); |
| 152 | + } |
| 153 | + }; |
112 | 154 | markers.push({ s, marker, overlay, element: container });
|
113 | 155 | }
|
114 | 156 |
|
|
165 | 207 | <div id="map"></div>
|
166 | 208 |
|
167 | 209 | {#if selectedTrip}
|
168 |
| - <RouteMap map={map} tripId={selectedTrip.tripId} /> |
| 210 | + <RouteMap {map} tripId={selectedTrip.tripId} /> |
169 | 211 | {/if}
|
170 | 212 |
|
171 | 213 | <LocationButton on:locationObtained={handleLocationObtained} />
|
|
0 commit comments