Calculate the distance between two sets of latitude and longitude coordinates

The code defines a function called DISTANCE_BETWEEN that calculates the distance between two geographical coordinates on the Earth’s surface using the Haversine formula. Here’s a step-by-step breakdown of the code: Define a constant earthRadius with a value of 6371, representing the Earth’s radius in kilometers. Calculate the difference in latitude (dLat) and longitude (dLon) between the two coordinates. These differences are obtained by subtracting the respective initial coordinates (lat1 and lon1) from the final coordinates (lat2 and lon2). Convert the differences in latitude and longitude from degrees to radians by calling the degreesToRadians function with the respective differences as arguments. The degreesToRadians function converts degrees to radians using the formula deg * Math.PI / 180. Use the Haversine formula to calculate the distance between the two coordinates. The Haversine formula involves several trigonometric calculations. Here’s the breakdown of the formula: a. Calculate a using the following equation: arduino Copy code a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(degreesToRadians(lat1)) * Math.cos(degreesToRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); This equation represents the squared value of the Haversine function applied to the angular differences. It involves computing the squares of sine and cosine functions. b. Calculate c using the following equation: arduino Copy code c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 – a)); The atan2 function calculates the arctangent of the ratio of two numbers. In this case, it computes the arctangent of the square root of a divided by the square root of 1 – a. The result represents the angular distance in radians. c. Calculate distance by multiplying earthRadius with c. This step converts the angular distance to a physical distance in kilometers. Finally, return the calculated distance value as the result of the function. Additionally, the code includes a helper function called degreesToRadians, which converts degrees to radians by multiplying the input value (deg) by Math.PI / 180. This function is used to convert the latitude and longitude differences from degrees to radians. In summary, the DISTANCE_BETWEEN function calculates the distance (in kilometers) between two sets of latitude and longitude coordinates using the Haversine formula. It leverages the degreesToRadians helper function to convert degrees to radians and performs a series of trigonometric calculations to determine the distance. In summary, this function takes a string of comma-separated values (range) and a threshold value (threshold), and calculates the average of the numbers in the range that are above or equal to the threshold. If there are no numbers above the threshold, it returns 0. Course content web development and web design courses with coding examples and source code for the lesson content. Source Code is available within my Github account. Lessons posted are designed to help students learn more about a specific topic related to modern web development and applying code. Laurence Svekis is a professional top selling course author having instructed over 1 Million students both online and in person. Laurence Svekis is a Google Developer Expert specializing in Google Workspace automation using Google Apps Script Code. Check out my Website for more details at https://basescripts.com/ Subscribe: https://www.youtube.com/@LaurenceSvekisCourses Tags and Keywords #HigherOrderFunctions #Arrays #Map #Filter #Reduce #JavaScript #DynamicCode #DoubleNumbers #EvenNumbers #OddNumbers #SmallNumbers #SumOfValues #FunctionalProgramming #NewArrays #ExistingArrays #FundamentalConcepts #FunctionalMethods #RepeatFunction #InvokeFunction #ConsoleLog Copy data function #ParameterFunctions #RobustTemplates #FlexibleValues #DynamicFunctions #HigherOrderLogic #ArrayManipulation #FunctionalArrays #CodeProcessing #EfficientLogic #FlexibleCode #DynamicLogic #FunctionalLogic #HigherOrderFunctions101 #Map101 #Filter101 #Reduce101 #JavaScript101 #ArrayProcessing #FunctionalArrayMethods #JavaScriptDevelopment #DynamicDevelopment #EfficientDevelopment #HigherOrderDevelopment #MapLogic #FilterLogic #ReduceLogic #JavaScriptLogic #ArrayOperations #FunctionalOperations #DynamicOperations #EfficientOperations #HigherOrderOperations #FunctionalProgramming101 #HigherOrderFunctionsExplained #ArraysExplained #MapExplained #FilterExplained #ReduceExplained #tips