-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
32 lines (28 loc) · 928 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { getCurrentPosition } from './utils/geolocation.js'
const getLocationButton = document.querySelector('#get-location');
getLocationButton.addEventListener('click', async (e) => {
if (!navigator.geolocation) {
alert("Geolocation not available. Please choose another method.");
return;
}
try {
const coords = (await getCurrentPosition())?.coords;
console.info("Got location:", coords);
redirectToLocation(getLocation(coords))
}
catch (error) {
alert(error.message);
}
});
function getLocation(coords, { decimals = 2 } = {}) {
const { latitude, longitude } = coords;
const lat = latitude.toFixed(decimals);
const lon = longitude.toFixed(decimals);
return `${lat},${lon}`
}
function redirectToLocation(location) {
const pathname = `./posts/`;
const url = new URL(pathname, window.location);
url.search = new URLSearchParams({ location });
window.location.href = url;
}