Localized geocoded coordinate formatting

This commit is contained in:
Minh Nguyễn
2018-01-09 02:23:13 -08:00
parent a1e0d4fc08
commit fe8c218fb3
2 changed files with 19 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import { d3keybinding as d3_keybinding } from '../lib/d3.keybinding.js';
import * as sexagesimal from '@mapbox/sexagesimal';
import { t } from '../util/locale';
import { displayCoordinatePair } from '../util/units';
import { geoExtent, geoChooseEdge } from '../geo';
import { modeSelect } from '../modes';
import { osmEntity } from '../osm';
@@ -143,7 +144,7 @@ export function uiFeatureList(context) {
id: -1,
geometry: 'point',
type: t('inspector.location'),
name: loc[0].toFixed(6) + ', ' + loc[1].toFixed(6),
name: displayCoordinatePair([loc[1], loc[0]]),
location: loc
});
}

View File

@@ -4,6 +4,12 @@ import { utilDetect } from 'detect';
var OSM_PRECISION = 7;
var locale = utilDetect().locale;
/**
* Returns a localized representation of the given length measurement.
*
* @param {Number} m area in meters
* @param {Boolean} isImperial true for U.S. customary units; false for metric
*/
export function displayLength(m, isImperial) {
var d = m * (isImperial ? 3.28084 : 1),
unit;
@@ -29,6 +35,12 @@ export function displayLength(m, isImperial) {
});
}
/**
* Returns a localized representation of the given area measurement.
*
* @param {Number} m2 area in square meters
* @param {Boolean} isImperial true for U.S. customary units; false for metric
*/
export function displayArea(m2, isImperial) {
var d = m2 * (isImperial ? 10.7639111056 : 1),
d1, d2, unit1, unit2, area;
@@ -87,6 +99,11 @@ function clamp(x, min, max) {
return Math.max(min, Math.min(x, max));
}
/**
* Returns a human-readable representation of the given coordinate pair.
*
* @param {Array<Number>} coord longitude and latitude
*/
export function displayCoordinatePair(coord) {
return t('units.coordinate_pair', {
latitude: clamp(coord[1], -90, 90).toLocaleString(locale, { maximumFractionDigits: OSM_PRECISION }),