Checking my math for distance between 2 points in miles

120
January 16, 2022, at 04:10 AM

Hello I am trying to do a calculator of finding the distance between 2 points. Everything works properly. I just want to make sure that my math is correct for the calculations. I will give you 2 coordinates to plug in and tell me if it is right or wrong. Thanks.

Test Coordinates: lat1: 27.95094 lng1: -82.45404

lat2: 27.9659 lng2: -82.8001

Test Results:

21.13 Miles

I just want to check that I am getting the correct calculations from 1 point to the next or if my math is incorrect. Thanks.

function distance(lat1, lng1, lat2, lng2, unit) {
                var radlat1 = Math.PI * lat1/180;
                var radlat2 = Math.PI * lat2/180;
                var theta = lng1-lng2;
                var radtheta = Math.PI * theta/180;
                var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
                if (dist > 1) {
                    dist = 1;
                }
                dist = Math.acos(dist);
                dist = dist * 180/Math.PI;
                dist = dist * 60 * 1.1515;
                if (unit=='miles') { dist = (dist * 0.8684) * 1.15077945 }
                return dist;
            }
       travel = distance(lat1, lng1, results.features[0].center[1], results.features[0].center[0], 'miles');
       travel = parseInt(travel * 100)/100;
Answer 1

Yes, you seem to have implemented the Haversine formula correctly ( https://en.wikipedia.org/wiki/Haversine_formula )

Rent Charter Buses Company
READ ALSO
How can I make a Horizontal scrolling nav bar that also works with mobile

How can I make a Horizontal scrolling nav bar that also works with mobile

So I have a website about the NBA and the problem is there is 30 teams in the NBAWhen I make a nav bar it takes up half the page because there is 30 teams lol

68
Mongodb updateMany() not working in nodejs

Mongodb updateMany() not working in nodejs

I have a collection called imported_products with documents as following,

101
How to use regular expression in ObjectBox

How to use regular expression in ObjectBox

I want to implement regular expression in ObjectBox databaseSomething like -

107
Add validation colour in Javascript forms [closed]

Add validation colour in Javascript forms [closed]

Want to improve this question? Update the question so it's on-topic for Stack Overflow

96