@@ -3,11 +3,13 @@ import React, { useEffect, useState } from "react";
33
44export default function Page ( ) {
55 const [ positions , setPositions ] = useState < GeolocationPosition [ ] > ( [ ] ) ;
6+ const [ updateCount , setUpdateCount ] = useState < number > ( 0 ) ;
67
78 useEffect ( ( ) => {
89 const watchID = navigator . geolocation . watchPosition (
910 ( position ) => {
10- setPositions ( [ ...positions , position ] ) ;
11+ setPositions ( [ position , ...positions ] ) ;
12+ setUpdateCount ( updateCount + 1 ) ;
1113 } ,
1214 ( ) => { } ,
1315 { enableHighAccuracy : true }
@@ -19,6 +21,26 @@ export default function Page() {
1921 } , [ setPositions ] ) ;
2022
2123 return ( < div >
22- { JSON . stringify ( positions ) }
24+ < div > { updateCount } </ div >
25+ { positions . map ( ( pos , index ) => {
26+ const coords = pos . coords
27+ const coordEntries : [ keyof GeolocationCoordinates , number | null ] [ ] = [
28+ [ 'latitude' , coords . latitude ] ,
29+ [ 'longitude' , coords . longitude ] ,
30+ [ 'altitude' , coords . altitude ] ,
31+ [ 'accuracy' , coords . accuracy ] ,
32+ [ 'altitudeAccuracy' , coords . altitudeAccuracy ] ,
33+ [ 'heading' , coords . heading ] ,
34+ [ 'speed' , coords . speed ] ,
35+ ] ;
36+
37+ return < div style = { { border : "1px solid white" , padding : "10px" } } key = { index } >
38+ { coordEntries . map ( ( [ key , value ] ) => (
39+ < div key = { key } >
40+ < strong > { key } :</ strong > { value === null ? 'N/A' : value }
41+ </ div >
42+ ) ) }
43+ </ div >
44+ } ) }
2345 </ div > ) ;
2446}
0 commit comments