Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/disable-markers-appear-inShowRouteMode #143

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/components/map/MapView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@
}

function updateMarkers() {
if (showRoute) {
const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id));
stopsToShow.forEach((s) => addMarker(s));
} else if (!selectedRoute && !isTripPlanModeActive) {
if (!selectedRoute && !isTripPlanModeActive) {
allStops.forEach((s) => addMarker(s));
}
}
Expand Down
31 changes: 22 additions & 9 deletions src/components/map/RouteMap.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script>
import { calculateMidpoint } from '$lib/mathUtils';
import { clearVehicleMarkersMap, fetchAndUpdateVehicles } from '$lib/vehicleUtils';
import { onMount, onDestroy } from 'svelte';
export let mapProvider;
Expand All @@ -11,19 +12,26 @@

// used to clear interval api calls
let currentIntervalId = null;
let loadRouteDataPromise = null;

onMount(async () => {
await loadRouteData();
loadRouteDataPromise = loadRouteData();
await loadRouteDataPromise;
});

onDestroy(async () => {
isMounted = false;
mapProvider.removePolyline(await polyline);
mapProvider.removeStopMarkers();
mapProvider.cleanupInfoWindow();
clearInterval(currentIntervalId);
clearVehicleMarkersMap(mapProvider);
mapProvider.clearVehicleMarkers();
if (loadRouteDataPromise) {
await loadRouteDataPromise;
}
await Promise.all([
mapProvider.removePolyline(await polyline),
mapProvider.removeStopMarkers(),
mapProvider.cleanupInfoWindow(),
clearInterval(currentIntervalId),
clearVehicleMarkersMap(mapProvider),
mapProvider.clearVehicleMarkers()
]);
});

async function loadRouteData() {
Expand All @@ -40,14 +48,17 @@
const shapeResponse = await fetch(`/api/oba/shape/${shapeId}`);
shapeData = await shapeResponse.json();
const shapePoints = shapeData?.data?.entry?.points;

if (shapePoints && isMounted) {
polyline = await mapProvider.createPolyline(shapePoints);
}
}

const stopTimes = tripData?.data?.entry?.schedule?.stopTimes ?? [];
const stops = tripData?.data?.references?.stops ?? [];
// TODO: implement better way to transition to route shape
const location = calculateMidpoint(stops);

mapProvider.flyTo(location.lat, location.lng, 13);

for (const stopTime of stopTimes) {
const stop = stops.find((s) => s.id === stopTime.stopId);
Expand All @@ -56,6 +67,8 @@
}
}

currentIntervalId = await fetchAndUpdateVehicles(routeId, mapProvider);
if (routeId && isMounted) {
currentIntervalId = await fetchAndUpdateVehicles(routeId, mapProvider);
}
}
</script>
4 changes: 4 additions & 0 deletions src/lib/Provider/GoogleMapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ export default class GoogleMapProvider {
this.map.panTo({ lat, lng });
}

flyTo(lat, lng, zoom = 15) {
this.map.setZoom(zoom);
this.map.setCenter({ lat, lng });
}
setZoom(zoom) {
this.map.setZoom(zoom);
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib/Provider/OpenStreetMapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,11 @@ export default class OpenStreetMapProvider {
this.map.panTo([lat, lng]);
}

flyTo(lat, lng, zoom = 15) {
if (!browser || !this.map) return;
this.map.flyTo([lat, lng], zoom);
}

setZoom(zoom) {
if (!browser || !this.map) return;
this.map.setZoom(zoom);
Expand Down
Loading