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;
Yes, you seem to have implemented the Haversine formula correctly (https://en.wikipedia.org/wiki/Haversine_formula)