1
1
import React , { useState , useEffect , useRef } from "react" ;
2
- import { useComponents } from "@wq/react" ;
2
+ import { useComponents , Message } from "@wq/react" ;
3
3
import PropTypes from "prop-types" ;
4
4
5
- export default function GeoLocate ( { type, setLocation } ) {
5
+ export default function GeoLocate ( { type, setLocation, value , accuracy } ) {
6
6
const { Button, Typography, useGeolocation } = useComponents ( ) ,
7
7
geolocation = useGeolocation ( ) ,
8
8
[ gpsStatus , setGpsStatus ] = useState ( "" ) ,
@@ -37,15 +37,7 @@ export default function GeoLocate({ type, setLocation }) {
37
37
save : type === "geopoint" ,
38
38
} ) ;
39
39
40
- const latFmt = lat > 0 ? lat + "°N" : - lat + "°S" ,
41
- lngFmt = lng > 0 ? lng + "°E" : - lng + "°W" ,
42
- accFmt =
43
- acc > 1000
44
- ? "~" + Math . round ( acc / 1000 ) + "km"
45
- : acc > 1
46
- ? "~" + Math . round ( acc ) + "m"
47
- : acc + "m" ;
48
- setGpsStatus ( `${ latFmt } ${ lngFmt } (${ accFmt } )` ) ;
40
+ setGpsStatus ( formatLoc ( lat , lng , acc ) ) ;
49
41
}
50
42
51
43
function onError ( error ) {
@@ -69,7 +61,12 @@ export default function GeoLocate({ type, setLocation }) {
69
61
return ( ) => stopGps ( ) ;
70
62
} , [ ] ) ;
71
63
72
- const gpsActive = ! ! gpsWatch . current ;
64
+ const gpsActive = ! ! gpsWatch . current ,
65
+ valueStatus =
66
+ value &&
67
+ value . type === "Point" &&
68
+ value . coordinates &&
69
+ formatLoc ( value . coordinates [ 1 ] , value . coordinates [ 0 ] , accuracy ) ;
73
70
74
71
return (
75
72
< >
@@ -81,7 +78,7 @@ export default function GeoLocate({ type, setLocation }) {
81
78
} }
82
79
color = "textSecondary"
83
80
>
84
- { gpsStatus }
81
+ { gpsStatus || valueStatus || "" }
85
82
</ Typography >
86
83
< Button
87
84
icon = { gpsActive ? "gps-stop" : "gps-start" }
@@ -90,7 +87,7 @@ export default function GeoLocate({ type, setLocation }) {
90
87
color = "secondary"
91
88
onClick = { gpsActive ? resetGps : startGps }
92
89
>
93
- { gpsActive ? "Stop GPS " : "Start GPS" }
90
+ < Message id = { gpsActive ? "GEO_STOP_GPS " : "GEO_START_GPS" } />
94
91
</ Button >
95
92
</ >
96
93
) ;
@@ -102,3 +99,17 @@ GeoLocate.propTypes = {
102
99
type : PropTypes . string ,
103
100
setLocation : PropTypes . func ,
104
101
} ;
102
+
103
+ function formatLoc ( lat , lng , acc ) {
104
+ const latFmt = lat > 0 ? lat + "°N" : - lat + "°S" ,
105
+ lngFmt = lng > 0 ? lng + "°E" : - lng + "°W" ,
106
+ accFmt =
107
+ acc > 1000
108
+ ? " (~" + Math . round ( acc / 1000 ) + "km)"
109
+ : acc > 1
110
+ ? " (~" + Math . round ( acc ) + "m)"
111
+ : acc
112
+ ? " (" + acc + "m)"
113
+ : "" ;
114
+ return `${ latFmt } ${ lngFmt } ${ accFmt } ` ;
115
+ }
0 commit comments