Ich verwende eine Funktion, die im Dateipfad server/utils/cardinalpoint.js gespeichert ist:
/**
* Given "0-360" returns the nearest cardinal direction "N/NE/E/SE/S/SW/W/NW"
*/
function getCardinalDirection(angle) {
if (typeof angle === 'string') {
angle = parseInt(angle);
}
if (angle <= 0 || angle > 360 || typeof angle === 'undefined') {
return '☈';
}
const arrows = { north: '↑ N', north_north_east: '↗ NNE', north_east: '↗ NE', east: '→ E', south_east: '↘ SE', south_south_east: '↘ SSE', south: '↓ S', south_south_west: '↙ SSW', south_west: '↙ SW', west: '← W', north_west: '↖ NW', north_north_west: '↖ NNW' };
const directions = Object.keys(arrows);
const degree = 360 / directions.length;
angle = angle + degree / 2;
for (let i = 0; i < directions.length; i++) {
if (angle >= (i * degree) && angle < (i + 1) * degree) {
return arrows[directions[i]];
}
}
return arrows['north'];
}
module.exports = {
getCardinalDirection,
};
Für die Windrichtung gibt es 16 mögliche Richtungen. Ich fand es einfacher, den Wert als String in der DB abzurufen, aber ich hatte nicht daran gedacht, dies in der UI zu tun. Ich werde es auf der Frontend-Seite ändern.