Merge branch 'main' of https://repos.hackathon.bz.it/2025-summer/team-1
This commit is contained in:
commit
f05b5666db
3 changed files with 100 additions and 0 deletions
62
frontend/src/lib/gps.ts
Normal file
62
frontend/src/lib/gps.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
export type Coordinates = {
|
||||
latitude: number
|
||||
longitude: number
|
||||
}
|
||||
|
||||
function geolocation_to_simple_coords(coordinates: GeolocationCoordinates): Coordinates {
|
||||
return { latitude: coordinates.latitude, longitude: coordinates.longitude }
|
||||
}
|
||||
|
||||
export function get_coords(): Promise<{ coords: Coordinates | null, error: string | null }> {
|
||||
return new Promise((resolve) => {
|
||||
if (!navigator.geolocation) {
|
||||
resolve({ coords: null, error: "Geolocation is not supported by your browser" })
|
||||
return
|
||||
}
|
||||
|
||||
const error_callback = (gps_error: GeolocationPositionError) => {
|
||||
console.log(gps_error)
|
||||
resolve({
|
||||
coords: null,
|
||||
error: `Unable to retrieve your location: (${gps_error.message})`
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const success_callback = (gps_position: GeolocationPosition) => {
|
||||
resolve({
|
||||
coords: geolocation_to_simple_coords(gps_position.coords),
|
||||
error: null
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
navigator.geolocation.getCurrentPosition(success_callback, error_callback)
|
||||
})
|
||||
}
|
||||
|
||||
function distance_between_coords(
|
||||
lhs: Coordinates,
|
||||
rhs: Coordinates
|
||||
): number {
|
||||
const R = 6371000; // earth radius in meters
|
||||
const to_rad = (deg: number) => (deg * Math.PI) / 180;
|
||||
|
||||
const d_lat = to_rad(rhs.latitude - lhs.latitude);
|
||||
const d_lon = to_rad(rhs.longitude - lhs.longitude);
|
||||
|
||||
const a =
|
||||
Math.sin(d_lat / 2) ** 2 +
|
||||
Math.cos(to_rad(lhs.latitude)) *
|
||||
Math.cos(to_rad(rhs.latitude)) *
|
||||
Math.sin(d_lon / 2) ** 2;
|
||||
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
|
||||
return R * c; // distance in meters
|
||||
}
|
||||
|
||||
export function is_within_range(my_coords: Coordinates, target_coords: Coordinates, max_range: number): boolean {
|
||||
const distance = distance_between_coords(my_coords, target_coords)
|
||||
return distance <= max_range
|
||||
}
|
17
frontend/src/routes/zesty/gps/+page.svelte
Normal file
17
frontend/src/routes/zesty/gps/+page.svelte
Normal file
|
@ -0,0 +1,17 @@
|
|||
<script lang="ts">
|
||||
import { onMount } from "svelte"
|
||||
import { get_coords, is_within_range, type Coordinates } from "$lib/gps"
|
||||
|
||||
let lido_schenna_coords: Coordinates = { latitude: 46.6769043, longitude: 11.1851585 }
|
||||
|
||||
onMount(async () => {
|
||||
let { coords, error } = await get_coords()
|
||||
console.log(coords)
|
||||
if (error != null) {
|
||||
return console.log(error)
|
||||
}
|
||||
if (coords == null) return
|
||||
|
||||
console.log(is_within_range(coords, lido_schenna_coords, 103))
|
||||
})
|
||||
</script>
|
21
frontend/static/manifest.json
Normal file
21
frontend/static/manifest.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name": "Chillbox Music Player",
|
||||
"short_name": "Chillbox",
|
||||
"start_url": "/",
|
||||
"display": "standalone",
|
||||
"background_color": "#334155",
|
||||
"theme_color": "#334155",
|
||||
"orientation": "portrait-primary",
|
||||
"icons": [
|
||||
{
|
||||
"src": "/icon-512.png",
|
||||
"sizes": "512x512",
|
||||
"type": "image/png"
|
||||
},
|
||||
{
|
||||
"src": "/icon-192.png",
|
||||
"sizes": "192x192",
|
||||
"type": "image/png"
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue