mod mooved gps functions from front end to back end
This commit is contained in:
parent
0066166765
commit
46b055c591
2 changed files with 30 additions and 29 deletions
27
backend/src/gps.py
Normal file
27
backend/src/gps.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
import math
|
||||
|
||||
|
||||
class Coordinates:
|
||||
latitude: int
|
||||
longitude: int
|
||||
|
||||
|
||||
def distance_between_coords(lhs: Coordinates, rhs: Coordinates) -> float:
|
||||
R = 6371000 # Earth radius in meters
|
||||
|
||||
def to_rad(deg):
|
||||
return (deg * math.pi) / 180
|
||||
|
||||
d_lat = to_rad(rhs.latitude - lhs.latitude)
|
||||
d_lon = to_rad(rhs.longitude - lhs.longitude)
|
||||
|
||||
a = (d_lat / 2) ** 2 + (to_rad(lhs.latitude) * to_rad(rhs.latitude)) * (d_lon / 2) ** 2
|
||||
|
||||
c = 2 * (a**0.5) / ((1 - a) ** 0.5)
|
||||
|
||||
return R * c # distance in meters
|
||||
|
||||
|
||||
def is_within_range(my_coords: Coordinates, target_coords: Coordinates, max_range: float) -> bool:
|
||||
distance = distance_between_coords(my_coords, target_coords)
|
||||
return distance <= max_range
|
|
@ -7,7 +7,7 @@ function geolocation_to_simple_coords(coordinates: GeolocationCoordinates): Coor
|
|||
return { latitude: coordinates.latitude, longitude: coordinates.longitude }
|
||||
}
|
||||
|
||||
export function get_coords(): Promise<{ coords: Coordinates | null, error: string | null }> {
|
||||
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" })
|
||||
|
@ -18,7 +18,7 @@ export function get_coords(): Promise<{ coords: Coordinates | null, error: strin
|
|||
console.log(gps_error)
|
||||
resolve({
|
||||
coords: null,
|
||||
error: `Unable to retrieve your location: (${gps_error.message})`
|
||||
error: `Unable to retrieve your location: (${gps_error.message})`,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ export function get_coords(): Promise<{ coords: Coordinates | null, error: strin
|
|||
const success_callback = (gps_position: GeolocationPosition) => {
|
||||
resolve({
|
||||
coords: geolocation_to_simple_coords(gps_position.coords),
|
||||
error: null
|
||||
error: null,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
@ -34,29 +34,3 @@ export function get_coords(): Promise<{ coords: Coordinates | null, error: strin
|
|||
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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue