Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added configuration option to choose between metric and imperial units. #143

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ window.owntracks.config = {};
- [`minAccuracy`](#filtersminaccuracy)
- [`ignorePingLocation`](#ignorepinglocation)
- [`locale`](#locale)
- [`units`](#units)
jeremyakers marked this conversation as resolved.
Show resolved Hide resolved
- `map`
- [`attribution`](#mapattribution)
- [`circle`](#mapcircle)
Expand Down Expand Up @@ -189,6 +190,21 @@ use `en-US` for translations.
- Type: [`String`]
- Default: `"en-US"`

### `units`
jeremyakers marked this conversation as resolved.
Show resolved Hide resolved

Allows the configuration of the units of measurement to use for the user interface.

Available options:

- `metric`
- `imperial`

Choosing anything other than one of these options will fall back to metric.

- Type: [`String`]
- Default: `"metric"`


### `map.attribution`

Attribution for map tiles.
Expand Down
6 changes: 4 additions & 2 deletions src/components/LDeviceLocationPopup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<br />
{{ lon }}
<br />
{{ alt }}m
{{ $config.units == "imperial" ? Math.round(alt*3.28084) : alt }}
{{ $config.units == "imperial" ? " ft" : " m" }}
</li>
<li v-if="address" :title="$t('Address')">
<HomeIcon size="1x" aria-hidden="true" role="img" />
Expand All @@ -38,7 +39,8 @@
</li>
<li v-if="typeof speed === 'number'" :title="$t('Speed')">
<ZapIcon size="1x" aria-hidden="true" role="img" />
{{ speed }} km/h
{{ $config.units == "imperial" ? Math.round(speed/1.609*10)/10 : speed }}
{{ $config.units == "imperial" ? " mph" : " km/h" }}
</li>
<li v-if="wifi.ssid" :title="$t('WiFi')">
<WifiIcon size="1x" aria-hidden="true" role="img" />
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const DEFAULT_CONFIG = {
},
ignorePingLocation: true,
locale: "en-US",
units: "metric",
jeremyakers marked this conversation as resolved.
Show resolved Hide resolved
map: {
attribution:
'&copy; <a href="https://osm.org/copyright">OpenStreetMap</a> contributors',
Expand Down
29 changes: 21 additions & 8 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,34 @@ export const distanceBetweenCoordinates = (c1, c2) => {
/**
* Format a distance in meters into a human-readable string with unit.
*
* This only supports m / km for now, but could read a config option and return
* ft / mi.
*
* @param {Number} distance Distance in meters
* @returns {String} Formatted string including unit
*/
export const humanReadableDistance = (distance) => {
let unit = "m";
if (Math.abs(distance) >= 1000) {
distance = distance / 1000;
unit = "km";
let unit = 'm';
let digits = 1;

if(config.units == "imperial") {
jeremyakers marked this conversation as resolved.
Show resolved Hide resolved
unit = "ft";
digits = 0; // We don't need anything after the decimal when working with feet.
distance = distance * 3.28084; // convert meters to feet.
if(Math.abs(distance) >= 1500) { // Most mapping apps switch to fractions of a mile above 1,500 ft
distance = distance / 5280;
unit = "mi";
digits = 2;
}
}
else
{
if (Math.abs(distance) >= 1000) {
distance = distance / 1000;
unit = "km";
}
}
return `${distance.toLocaleString(config.locale, {
maximumFractionDigits: 1,
maximumFractionDigits: digits,
})} ${unit}`;

};

/**
Expand Down