52 lines
1.3 KiB
JavaScript
52 lines
1.3 KiB
JavaScript
// ============================================================================
|
|
// COORDINATE & DISTANCE FORMATTING UTILITIES
|
|
// ============================================================================
|
|
|
|
/**
|
|
* Format coordinate to 6 decimal places (±11cm precision)
|
|
*/
|
|
export function formatCoordinate(value, type) {
|
|
const dir = type === 'lat'
|
|
? (value >= 0 ? 'N' : 'S')
|
|
: (value >= 0 ? 'E' : 'W');
|
|
return `${Math.abs(value).toFixed(6)}°${dir}`;
|
|
}
|
|
|
|
/**
|
|
* Format distance in meters or feet based on unit preference
|
|
*/
|
|
export function formatDistance(meters, useImperial = false) {
|
|
if (useImperial) {
|
|
const feet = meters * 3.28084;
|
|
if (feet < 5280) {
|
|
return `${feet.toFixed(1)} ft`;
|
|
} else {
|
|
const miles = feet / 5280;
|
|
return `${miles.toFixed(2)} mi`;
|
|
}
|
|
} else {
|
|
if (meters < 1000) {
|
|
return `${meters.toFixed(1)} m`;
|
|
} else {
|
|
const km = meters / 1000;
|
|
return `${km.toFixed(2)} km`;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Format bearing angle
|
|
*/
|
|
export function formatBearing(degrees) {
|
|
return `${degrees.toFixed(1)}°`;
|
|
}
|
|
|
|
/**
|
|
* Convert web Mercator to lat/long
|
|
*/
|
|
export function webMercatorToLonLat(x, y) {
|
|
const R = 6378137;
|
|
const lon = (x / R) * (180 / Math.PI);
|
|
const lat = (2 * Math.atan(Math.exp(y / R)) - Math.PI / 2) * (180 / Math.PI);
|
|
return [lon, lat];
|
|
} |