diff --git a/src/lib/islands/near-to-me-island.svelte b/src/lib/islands/near-to-me-island.svelte index b0825f3..87d85e0 100644 --- a/src/lib/islands/near-to-me-island.svelte +++ b/src/lib/islands/near-to-me-island.svelte @@ -3,6 +3,7 @@ import { apiGet } from "$lib/scripts/apiFetch"; import { getCurrentLocation } from "$lib/scripts/getLocation"; import { location } from "$lib/stores/location"; + //import { NearestStationResponse } from "@owlboard/ts-types"; export let variables = { title: "Near to Me", }; @@ -13,28 +14,34 @@ async function getNearestStations() { const currLocation = await getCurrentLocation(); + console.debug(`Current Latitude: ${currLocation.latitude}, Current Longitude: ${currLocation.longitude}`) const apiPath = `/api/v2/live/station/nearest/${currLocation.latitude}/${currLocation.longitude}` const res = await apiGet(apiPath) + console.log(res) + return res as any[] } {#if !$location} -

Coming soon

- + --> {:else if $location} {#await getNearestStations()}

Fetching Location

- {:then stations} + {:then stations} +
{#each stations as station} - {station.NLCDESC} - {station.miles}mi + {station.NLCDESC} - {station.miles}mi {/each} +
{:catch err}

Error: {err.message}

- {/await} + {/await} {/if}
@@ -57,5 +64,28 @@ box-shadow: var(--box-shadow); } - + #buttons { + display: flex; + flex-wrap: wrap; + justify-content: center; + align-items: center; + width: 95%; + margin: auto; + padding-top: 5px; + } + .link { + display: inline-flex; + margin: 5px; + border: none; + border-radius: 20px; + padding: 5px 10px; + font-family: urwgothic, "Lucida Sans", "Lucida Sans Regular", "Lucida Grande", "Lucida Sans Unicode", Geneva, Verdana, sans-serif; + font-size: 16px; + font-weight: 400; + text-decoration: none; + background-color: var(--island-button-color); + color: var(--island-link-color); + box-shadow: var(--box-shadow); + white-space: nowrap; + } \ No newline at end of file diff --git a/src/lib/scripts/getLocation.ts b/src/lib/scripts/getLocation.ts index 99ae984..01af2f7 100644 --- a/src/lib/scripts/getLocation.ts +++ b/src/lib/scripts/getLocation.ts @@ -1,20 +1,35 @@ -export function getCurrentLocation(): Promise { +export async function getCurrentLocation(): Promise { + console.debug("Fetching location"); + + if (typeof window === 'undefined') { + console.error("Location fetch has run serverside - invalid method") + } + + if (!navigator.geolocation) { + console.error("Geolocation is not supported"); + throw new Error("Geolocation is not supported"); + } + return new Promise((resolve, reject) => { - if (navigator.geolocation) { - navigator.geolocation.getCurrentPosition( - (position) => { - resolve({ - latitude: position.coords.latitude, - longitude: position.coords.longitude, - }); - }, - (error) => { - reject(error); - } - ); - } else { - reject(new Error("Geolocation is not supported by this browser.")); - } + const options = { + timeout: 10000, + maximumAge: 300, + }; + + navigator.geolocation.getCurrentPosition( + (position) => { + console.debug(`Position obtained: ${position.coords}`); + resolve({ + latitude: position.coords.latitude, + longitude: position.coords.longitude, + }); + }, + (error) => { + console.error("Error fetching location: ", error); + reject(error); + } + ); + options }); }