-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Minotriz02
authored and
Minotriz02
committed
Nov 13, 2024
1 parent
2d5e43f
commit 16cbc1a
Showing
5 changed files
with
89 additions
and
86 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export const getDistanceFromLatLonInKm = (lat1, lon1, lat2, lon2) => { | ||
const R = 6371; // Radius of the Earth in km | ||
const dLat = deg2rad(lat2 - lat1); | ||
const dLon = deg2rad(lon2 - lon1); | ||
const a = | ||
Math.sin(dLat / 2) * Math.sin(dLat / 2) + | ||
Math.cos(deg2rad(lat1)) * | ||
Math.cos(deg2rad(lat2)) * | ||
Math.sin(dLon / 2) * | ||
Math.sin(dLon / 2); | ||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | ||
return R * c; // Distance in km | ||
}; | ||
|
||
export const deg2rad = (deg) => deg * (Math.PI / 180); | ||
|
||
export const findNearestStation = (userLat, userLon, stations) => { | ||
let minDistance = Infinity; | ||
let nearest = null; | ||
|
||
stations.forEach((station) => { | ||
const distance = getDistanceFromLatLonInKm( | ||
userLat, | ||
userLon, | ||
station.latitude, | ||
station.longitude | ||
); | ||
if (distance < minDistance) { | ||
minDistance = distance; | ||
nearest = station; | ||
} | ||
}); | ||
|
||
return nearest; | ||
}; | ||
|
||
export const getDayOfWeek = (dateString) => { | ||
const date = new Date(dateString); | ||
return date.toLocaleDateString("es-ES", { weekday: "long" }); | ||
}; |