diff --git a/src/lib/scripts/getLocation.ts b/src/lib/scripts/getLocation.ts new file mode 100644 index 0000000..becc801 --- /dev/null +++ b/src/lib/scripts/getLocation.ts @@ -0,0 +1,19 @@ +export function getCurrentLocation() { + 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.")); + } + }); +} diff --git a/src/routes/more/statistics/+page.svelte b/src/routes/more/statistics/+page.svelte index 2092908..d43febe 100644 --- a/src/routes/more/statistics/+page.svelte +++ b/src/routes/more/statistics/+page.svelte @@ -5,6 +5,7 @@ import Nav from "$lib/navigation/nav.svelte"; import { getApiUrl } from "$lib/scripts/upstream"; import { featureDetect } from "$lib/scripts/featureDetect"; + import { getCurrentLocation } from "$lib/scripts/getLocation"; const title = "Statistics"; async function getData() { @@ -132,6 +133,17 @@ Failed to detect browser features: {error.message} {/await} +

Detected Location

+{#await getCurrentLocation()} + Fetching Location... +{:then location} + {location.latitude}, {location.longitude} +
+ View on Map +{:catch error} + {error.message} +{/await} +