/** * Converts numeric degrees to radians * @param val * @returns {number} */ function toRad (val: number): number { return val * Math.PI / 180 } /** * This function takes in latitude and longitude of two location and returns the distance between them as the crow flies (in km) * @param lat1 * @param lon1 * @param lat2 * @param lon2 * @returns {number} */ function sphericDistance (lat1: number, lon1: number, lat2: number, lon2: number): number { const R = 6371 // km const dLat = toRad(lat2 - lat1) const dLon = toRad(lon2 - lon1) lat1 = toRad(lat1) lat2 = toRad(lat2) const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2) const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)) return R * c } export default sphericDistance