UNPKG

atriusmaps-node-sdk

Version:

This project provides an API to Atrius Personal Wayfinder maps within a Node environment. See the README.md for more information

41 lines (38 loc) 1.12 kB
'use strict'; const toRadians = (d) => d * Math.PI / 180; function distance(lat1, lng1, lat2, lng2) { const R = 6371e3; const \u03C61 = toRadians(lat1); const \u03C62 = toRadians(lat2); const \u0394\u03C6 = toRadians(lat2 - lat1); const \u0394\u03BB = toRadians(lng2 - lng1); const a = Math.sin(\u0394\u03C6 / 2) * Math.sin(\u0394\u03C6 / 2) + Math.cos(\u03C61) * Math.cos(\u03C62) * Math.sin(\u0394\u03BB / 2) * Math.sin(\u0394\u03BB / 2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c; } function bearingToDirection(deg, T) { const names = [ T("wayfinder:north"), // 0 T("wayfinder:northeast"), // 45 T("wayfinder:east"), // 90 T("wayfinder:southeast"), // 135 T("wayfinder:south"), // 180 / -180 T("wayfinder:southwest"), // -135 T("wayfinder:west"), // -90 T("wayfinder:northwest") // -45 ]; const d = (deg + 180) % 360 - 180; const index = Math.round(d / 45); return names[(index + 8) % 8]; } exports.bearingToDirection = bearingToDirection; exports.distance = distance; exports.toRadians = toRadians;