Skip to content

Commit

Permalink
keep coordinates visible when GPS is off
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed May 24, 2024
1 parent 9b5c5ab commit 8548597
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
9 changes: 4 additions & 5 deletions packages/map/src/components/GeoTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import { useGeoTools } from "../hooks.js";
import PropTypes from "prop-types";

export default function GeoTools({ name, type, mapId }) {
const { toggleProps, setLocation, ActiveTool, value } = useGeoTools(
name,
type,
mapId
),
const { toggleProps, setLocation, ActiveTool, value, accuracy } =
useGeoTools(name, type, mapId),
{ View } = useComponents(),
{ Toggle } = useInputComponents(),
singleRow = useMinWidth(600);
Expand All @@ -29,6 +26,7 @@ export default function GeoTools({ name, type, mapId }) {
<ActiveTool
name={name}
value={value}
accuracy={accuracy}
type={type}
setLocation={setLocation}
/>
Expand All @@ -52,6 +50,7 @@ export default function GeoTools({ name, type, mapId }) {
<ActiveTool
name={name}
value={value}
accuracy={accuracy}
type={type}
setLocation={setLocation}
/>
Expand Down
39 changes: 25 additions & 14 deletions packages/map/src/geotools/GeoLocate.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useState, useEffect, useRef } from "react";
import { useComponents } from "@wq/react";
import { useComponents, Message } from "@wq/react";
import PropTypes from "prop-types";

export default function GeoLocate({ type, setLocation }) {
export default function GeoLocate({ type, setLocation, value, accuracy }) {
const { Button, Typography, useGeolocation } = useComponents(),
geolocation = useGeolocation(),
[gpsStatus, setGpsStatus] = useState(""),
Expand Down Expand Up @@ -37,15 +37,7 @@ export default function GeoLocate({ type, setLocation }) {
save: type === "geopoint",
});

const latFmt = lat > 0 ? lat + "°N" : -lat + "°S",
lngFmt = lng > 0 ? lng + "°E" : -lng + "°W",
accFmt =
acc > 1000
? "~" + Math.round(acc / 1000) + "km"
: acc > 1
? "~" + Math.round(acc) + "m"
: acc + "m";
setGpsStatus(`${latFmt} ${lngFmt} (${accFmt})`);
setGpsStatus(formatLoc(lat, lng, acc));
}

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

const gpsActive = !!gpsWatch.current;
const gpsActive = !!gpsWatch.current,
valueStatus =
value &&
value.type === "Point" &&
value.coordinates &&
formatLoc(value.coordinates[1], value.coordinates[0], accuracy);

return (
<>
Expand All @@ -81,7 +78,7 @@ export default function GeoLocate({ type, setLocation }) {
}}
color="textSecondary"
>
{gpsStatus}
{gpsStatus || valueStatus || ""}
</Typography>
<Button
icon={gpsActive ? "gps-stop" : "gps-start"}
Expand All @@ -90,7 +87,7 @@ export default function GeoLocate({ type, setLocation }) {
color="secondary"
onClick={gpsActive ? resetGps : startGps}
>
{gpsActive ? "Stop GPS" : "Start GPS"}
<Message id={gpsActive ? "GEO_STOP_GPS" : "GEO_START_GPS"} />
</Button>
</>
);
Expand All @@ -102,3 +99,17 @@ GeoLocate.propTypes = {
type: PropTypes.string,
setLocation: PropTypes.func,
};

function formatLoc(lat, lng, acc) {
const latFmt = lat > 0 ? lat + "°N" : -lat + "°S",
lngFmt = lng > 0 ? lng + "°E" : -lng + "°W",
accFmt =
acc > 1000
? " (~" + Math.round(acc / 1000) + "km)"
: acc > 1
? " (~" + Math.round(acc) + "m)"
: acc
? " (" + acc + "m)"
: "";
return `${latFmt} ${lngFmt}${accFmt}`;
}
7 changes: 5 additions & 2 deletions packages/map/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export function useGeoTools(name, type, mapId) {
{ useMapInstance } = useComponents(),
instance = useMapInstance(mapId),
[, { value }, { setValue }] = useField(name),
[, , { setValue: setAccuracy }] = useField(`${name}_accuracy`),
[, { value: accuracy }, { setValue: setAccuracy }] = useField(
`${name}_accuracy`
),
[, { value: activeTool }, { setValue: setActiveTool }] =
useField(toggleName);

Expand Down Expand Up @@ -102,8 +104,9 @@ export function useGeoTools(name, type, mapId) {
setLocation,
ActiveTool,
value,
accuracy,
}),
[toggleName, tools, setLocation, ActiveTool, value]
[toggleName, tools, setLocation, ActiveTool, value, accuracy]
);
}

Expand Down

0 comments on commit 8548597

Please sign in to comment.