11import { useEffect , useState } from "react" ;
22import { 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}
0 commit comments