Skip to content

Commit 8548597

Browse files
committed
keep coordinates visible when GPS is off
1 parent 9b5c5ab commit 8548597

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

packages/map/src/components/GeoTools.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ import { useGeoTools } from "../hooks.js";
55
import PropTypes from "prop-types";
66

77
export default function GeoTools({ name, type, mapId }) {
8-
const { toggleProps, setLocation, ActiveTool, value } = useGeoTools(
9-
name,
10-
type,
11-
mapId
12-
),
8+
const { toggleProps, setLocation, ActiveTool, value, accuracy } =
9+
useGeoTools(name, type, mapId),
1310
{ View } = useComponents(),
1411
{ Toggle } = useInputComponents(),
1512
singleRow = useMinWidth(600);
@@ -29,6 +26,7 @@ export default function GeoTools({ name, type, mapId }) {
2926
<ActiveTool
3027
name={name}
3128
value={value}
29+
accuracy={accuracy}
3230
type={type}
3331
setLocation={setLocation}
3432
/>
@@ -52,6 +50,7 @@ export default function GeoTools({ name, type, mapId }) {
5250
<ActiveTool
5351
name={name}
5452
value={value}
53+
accuracy={accuracy}
5554
type={type}
5655
setLocation={setLocation}
5756
/>

packages/map/src/geotools/GeoLocate.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { useState, useEffect, useRef } from "react";
2-
import { useComponents } from "@wq/react";
2+
import { useComponents, Message } from "@wq/react";
33
import PropTypes from "prop-types";
44

5-
export default function GeoLocate({ type, setLocation }) {
5+
export default function GeoLocate({ type, setLocation, value, accuracy }) {
66
const { Button, Typography, useGeolocation } = useComponents(),
77
geolocation = useGeolocation(),
88
[gpsStatus, setGpsStatus] = useState(""),
@@ -37,15 +37,7 @@ export default function GeoLocate({ type, setLocation }) {
3737
save: type === "geopoint",
3838
});
3939

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));
4941
}
5042

5143
function onError(error) {
@@ -69,7 +61,12 @@ export default function GeoLocate({ type, setLocation }) {
6961
return () => stopGps();
7062
}, []);
7163

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);
7370

7471
return (
7572
<>
@@ -81,7 +78,7 @@ export default function GeoLocate({ type, setLocation }) {
8178
}}
8279
color="textSecondary"
8380
>
84-
{gpsStatus}
81+
{gpsStatus || valueStatus || ""}
8582
</Typography>
8683
<Button
8784
icon={gpsActive ? "gps-stop" : "gps-start"}
@@ -90,7 +87,7 @@ export default function GeoLocate({ type, setLocation }) {
9087
color="secondary"
9188
onClick={gpsActive ? resetGps : startGps}
9289
>
93-
{gpsActive ? "Stop GPS" : "Start GPS"}
90+
<Message id={gpsActive ? "GEO_STOP_GPS" : "GEO_START_GPS"} />
9491
</Button>
9592
</>
9693
);
@@ -102,3 +99,17 @@ GeoLocate.propTypes = {
10299
type: PropTypes.string,
103100
setLocation: PropTypes.func,
104101
};
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+
}

packages/map/src/hooks.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export function useGeoTools(name, type, mapId) {
3535
{ useMapInstance } = useComponents(),
3636
instance = useMapInstance(mapId),
3737
[, { value }, { setValue }] = useField(name),
38-
[, , { setValue: setAccuracy }] = useField(`${name}_accuracy`),
38+
[, { value: accuracy }, { setValue: setAccuracy }] = useField(
39+
`${name}_accuracy`
40+
),
3941
[, { value: activeTool }, { setValue: setActiveTool }] =
4042
useField(toggleName);
4143

@@ -102,8 +104,9 @@ export function useGeoTools(name, type, mapId) {
102104
setLocation,
103105
ActiveTool,
104106
value,
107+
accuracy,
105108
}),
106-
[toggleName, tools, setLocation, ActiveTool, value]
109+
[toggleName, tools, setLocation, ActiveTool, value, accuracy]
107110
);
108111
}
109112

0 commit comments

Comments
 (0)