-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from OneBusAway/bus-route
Implemented the Bus Route, Draw polyline feat, and hidden unrelated stops
- Loading branch information
Showing
7 changed files
with
194 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<script> | ||
/* global google */ | ||
import { onMount, onDestroy } from 'svelte'; | ||
import { createPolyline, loadGoogleMapsLibrary } from '$lib/googleMaps'; | ||
export let map; | ||
export let tripId; | ||
let shapeId = null; | ||
let polyline; | ||
let stopMarkers = []; | ||
let infoWindow; | ||
let tripData = null; | ||
let shapeData = null; | ||
onMount(async () => { | ||
await loadGoogleMapsLibrary(); | ||
await loadRouteData(); | ||
}); | ||
onDestroy(() => { | ||
polyline?.setMap(null); | ||
stopMarkers.forEach((marker) => marker.setMap(null)); | ||
infoWindow?.close(); | ||
}); | ||
async function loadRouteData() { | ||
const tripResponse = await fetch(`/api/oba/trip-details/${tripId}`); | ||
tripData = await tripResponse.json(); | ||
const tripReferences = tripData?.data?.references?.trips; | ||
const moreTripData = tripReferences?.find((t) => t.id == tripId); | ||
shapeId = moreTripData?.shapeId; | ||
if (shapeId) { | ||
const shapeResponse = await fetch(`/api/oba/shape/${shapeId}`); | ||
shapeData = await shapeResponse.json(); | ||
const shape = shapeData?.data?.entry?.points; | ||
if (shape) { | ||
polyline = await createPolyline(shape); | ||
polyline.setMap(map); | ||
} | ||
} | ||
const stopTimes = tripData?.data?.entry?.schedule?.stopTimes ?? []; | ||
const stops = tripData?.data?.references?.stops ?? []; | ||
for (const stopTime of stopTimes) { | ||
const stop = stops.find((s) => s.id === stopTime.stopId); | ||
if (stop) { | ||
addStopMarker(stopTime, stop); | ||
} | ||
} | ||
} | ||
function addStopMarker(stopTime, stop) { | ||
const marker = new google.maps.Marker({ | ||
position: { lat: stop.lat, lng: stop.lon }, | ||
map: map, | ||
icon: { | ||
path: google.maps.SymbolPath.CIRCLE, | ||
scale: 5, | ||
fillColor: '#FFFFFF', | ||
fillOpacity: 1, | ||
strokeWeight: 1, | ||
strokeColor: '#000000' | ||
} | ||
}); | ||
marker.addListener('click', () => { | ||
infoWindow?.close(); | ||
infoWindow = new google.maps.InfoWindow({ | ||
content: `<div> | ||
<h3>${stop.name}</h3> | ||
<p>Arrival time: ${new Date(stopTime.arrivalTime * 1000).toLocaleTimeString()}</p> | ||
</div>` | ||
}); | ||
infoWindow.open(map, marker); | ||
}); | ||
stopMarkers.push(marker); | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { error, json } from '@sveltejs/kit'; | ||
import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public'; | ||
import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private'; | ||
|
||
export async function GET({ params }) { | ||
const { shapeId } = params; | ||
|
||
let apiURL = `${baseURL}/api/where/shape/${shapeId}.json?key=${apiKey}`; | ||
|
||
try { | ||
const response = await fetch(apiURL); | ||
console.log('response:', response); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
const data = await response.json(); | ||
return json(data); | ||
} catch (err) { | ||
console.error('Error fetching shape data:', err); | ||
throw error(500, 'Unable to fetch shape data.'); | ||
} | ||
} |