mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 01:31:39 -05:00
51 lines
1.3 KiB
JavaScript
51 lines
1.3 KiB
JavaScript
// javascript/maps/helpers.js
|
|
export function formatDistance(distance) {
|
|
if (distance < 1000) {
|
|
return `${distance.toFixed(2)} meters`;
|
|
} else {
|
|
return `${(distance / 1000).toFixed(2)} km`;
|
|
}
|
|
}
|
|
|
|
export function getUrlParameter(name) {
|
|
return new URLSearchParams(window.location.search).get(name);
|
|
}
|
|
|
|
export function minutesToDaysHoursMinutes(minutes) {
|
|
const days = Math.floor(minutes / (24 * 60));
|
|
const hours = Math.floor((minutes % (24 * 60)) / 60);
|
|
minutes = minutes % 60;
|
|
let result = "";
|
|
|
|
if (days > 0) {
|
|
result += `${days}d `;
|
|
}
|
|
|
|
if (hours > 0) {
|
|
result += `${hours}h `;
|
|
}
|
|
|
|
if (minutes > 0) {
|
|
result += `${minutes}min`;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
export function formatDate(timestamp, timezone) {
|
|
const date = new Date(timestamp * 1000);
|
|
return date.toLocaleString("en-GB", { timeZone: timezone });
|
|
}
|
|
|
|
export function haversineDistance(lat1, lon1, lat2, lon2) {
|
|
const toRad = (x) => (x * Math.PI) / 180;
|
|
const R = 6371; // Radius of the Earth in kilometers
|
|
const dLat = toRad(lat2 - lat1);
|
|
const dLon = toRad(lon2 - lon1);
|
|
const a =
|
|
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
|
|
Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
|
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
return R * c * 1000; // Distance in meters
|
|
}
|