Skip to content

Commit 6cef14e

Browse files
authored
Merge pull request #10 from Eternal-Encoders/gps
BugFix
2 parents aadaef2 + 450e2a5 commit 6cef14e

File tree

5 files changed

+66
-34
lines changed

5 files changed

+66
-34
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
1010
"lint:ts": "eslint \"**/*.{ts,tsx}\"",
1111
"lint:ts:fix": "eslint \"**/*.{ts,tsx}\" --fix",
12-
"lint:scss": "npx stylelint \"**/*.scss\"",
12+
"lint:scss": "npx stylelint \"**/*.scss\"",
1313
"lint:scss:fix": "npx stylelint \"**/*.scss\" --fix",
1414
"preview": "vite preview"
1515
},
@@ -21,6 +21,7 @@
2121
"konva": "^9.3.13",
2222
"react": "^18.2.0",
2323
"react-dom": "^18.2.0",
24+
"react-geolocated": "^4.3.0",
2425
"react-helmet-async": "^2.0.5",
2526
"react-i18next": "^15.0.1",
2627
"react-konva": "^18.2.10",

src/pages/institutes-page/InstitutesPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ function InstitutesPage() {
3232
}
3333

3434
const userLoc = useGpsHook(
35-
true,
3635
GPS_BUFFER
3736
)
3837

38+
console.log(userLoc)
39+
3940
dispatch(floorSet(instGps ? getClosestFloor(instGps, approxGps(userLoc)).floor : 1));
4041

4142
return (

src/shared/hooks/GpsHook.ts

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,66 @@
11
import { useEffect, useState } from "react";
22
import { UserGps } from "../../utils/interfaces";
3+
import { useGeolocated } from "react-geolocated";
34

4-
export function useGpsHook(isGpsAv: boolean, bufferSize: number) {
5+
export function useGpsHook(bufferSize: number) {
56
const [userLoc, setUserLoc] = useState(new Array<UserGps>(bufferSize));
67

7-
/* ON MOUNT - GET CURRENT GPS */
8-
useEffect(() => {
9-
if (navigator.geolocation && isGpsAv) {
10-
navigator.geolocation.getCurrentPosition(
11-
(pos) => {
12-
setUserLoc(userLoc.fill({
13-
latitude: pos.coords.latitude,
14-
longtitude: pos.coords.longitude,
15-
altitude: pos.coords.altitude ? pos.coords.altitude : 268.4
16-
}))
17-
}
18-
)
8+
const { coords } = useGeolocated({
9+
positionOptions: {
10+
enableHighAccuracy: true,
11+
maximumAge: 0
12+
},
13+
watchPosition: true,
14+
suppressLocationOnMount: false,
15+
geolocationProvider: navigator.geolocation,
16+
isOptimisticGeolocationEnabled: true,
17+
watchLocationPermissionChange: false,
18+
})
1919

20-
navigator.geolocation.watchPosition(
21-
(pos) => {
22-
console.log(userLoc);
23-
userLoc.shift()
24-
userLoc.push({
25-
latitude: pos.coords.latitude,
26-
longtitude: pos.coords.longitude,
27-
altitude: pos.coords.altitude ? pos.coords.altitude : 268.4
28-
})
29-
setUserLoc(userLoc);
30-
},
31-
() => {},
32-
{
33-
maximumAge: 0,
34-
enableHighAccuracy: true,
35-
}
36-
)
20+
useEffect(() => {
21+
if (coords) {
22+
userLoc.shift()
23+
userLoc.push({
24+
latitude: coords.latitude,
25+
longtitude: coords.longitude,
26+
altitude: coords.altitude ? coords.altitude : 268.4
27+
})
28+
setUserLoc(userLoc);
3729
}
38-
}, [navigator]);
30+
31+
}, [coords])
32+
33+
/* ON MOUNT - GET CURRENT GPS */
34+
// useEffect(() => {
35+
// if (navigator.geolocation && isGpsAv) {
36+
// navigator.geolocation.getCurrentPosition(
37+
// (pos) => {
38+
// setUserLoc(userLoc.fill({
39+
// latitude: pos.coords.latitude,
40+
// longtitude: pos.coords.longitude,
41+
// altitude: pos.coords.altitude ? pos.coords.altitude : 268.4
42+
// }))
43+
// }
44+
// )
45+
46+
// navigator.geolocation.watchPosition(
47+
// (pos) => {
48+
// userLoc.shift()
49+
// userLoc.push({
50+
// latitude: pos.coords.latitude,
51+
// longtitude: pos.coords.longitude,
52+
// altitude: pos.coords.altitude ? pos.coords.altitude : 268.4
53+
// })
54+
// setUserLoc(userLoc);
55+
// },
56+
// () => {},
57+
// {
58+
// maximumAge: 0,
59+
// enableHighAccuracy: true,
60+
// }
61+
// )
62+
// }
63+
// }, [navigator]);
3964

4065
return userLoc;
4166
}

src/utils/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const EQUATORIAL_RADIUS = 6378160;
4848

4949
const POLAR_RADIUS = 6356774;
5050

51-
const GPS_BUFFER = 4;
51+
const GPS_BUFFER = 2;
5252

5353
export {
5454
InstColors,

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2838,6 +2838,11 @@ react-fast-compare@^3.2.2:
28382838
resolved "https://registry.npmjs.org/react-fast-compare/-/react-fast-compare-3.2.2.tgz"
28392839
integrity sha512-nsO+KSNgo1SbJqJEYRE9ERzo7YtYbou/OqjSQKxV7jcKox7+usiUVZOAC+XnDOABXggQTno0Y1CpVnuWEc1boQ==
28402840

2841+
react-geolocated@^4.3.0:
2842+
version "4.3.0"
2843+
resolved "https://registry.yarnpkg.com/react-geolocated/-/react-geolocated-4.3.0.tgz#94565732e029624ea4c089660a24c544e256a68a"
2844+
integrity sha512-jj3x45VACcHHvSNWqWHf0f60V12pQDPIH6HuFyJi+BmndQ/dlE+vR0R2Eis+BYnwZPTlaEQ/ANmJjuUPH3Lxsg==
2845+
28412846
react-helmet-async@^2.0.5:
28422847
version "2.0.5"
28432848
resolved "https://registry.npmjs.org/react-helmet-async/-/react-helmet-async-2.0.5.tgz"

0 commit comments

Comments
 (0)