From 13f7163dd7075340d4f8a160b7400370d15e50f7 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 24 Jun 2024 00:06:30 +0100 Subject: [PATCH] Add location to statistics --- src/lib/scripts/getLocation.ts | 19 +++++++++++++++++++ src/routes/more/statistics/+page.svelte | 12 ++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/lib/scripts/getLocation.ts 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} +