/
Locate.js
38 lines (33 loc) · 979 Bytes
/
Locate.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
33
34
35
36
37
38
import * as Location from "expo-location";
import { ANDROID, WEB } from "./Constants";
export default function locate() {
if (WEB) {
return Location.getCurrentPositionAsync({
enableHighAccuracy: false,
maximumAge: 15 * 60 * 1000
});
} else {
return Location.getLastKnownPositionAsync();
}
}
export function distanceTo(item) {
return item.sort && item.sort[item.sort.length - 1];
}
export function roundedDistance(distance, withDot = true) {
const suffix = withDot ? " · " : "";
if (distance > 1000) {
const kMiles = (distance / 1000).toFixed(1);
return `${kMiles}kmi${suffix}`;
} else if (distance > 100) {
const miles = parseInt(distance, 10);
return `${miles}mi${suffix}`;
} else {
const miles = distance.toFixed(1);
return `${miles}mi${suffix}`;
}
}
export function roundedDistanceTo(item) {
const distance = distanceTo(item);
if (!distance) return null;
return roundedDistance(distance, false);
}