GeoPC Distance
Compute the great-circle distance between two geographic points
by GeoPostcodes
HTML
<!DOCTYPE html>
<html>
<body>
The great-circle distance between Brussels and Paris is:
<p><span id="dist"></span> Km.</p>
</body>
</html>
JavaScript
/*
Compute the great-circle distance between two geographic points
(C) 2019 by GeoPostcodes - GeoData Ltd
*/
brussels = {"lat":50.846710, "lng":4.352629};
paris = {"lat": 48.853286, "lng": 2.348792 };
document.getElementById("dist").innerHTML = distance(brussels, paris);
function distance(from, to)
{
var R = 6371; // Radius of the earth in km, can be changed to 3959 for miles
var dLat = deg2rad(to.lat - from.lat);
var dLon = deg2rad(to.lng - from.lng);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(from.lat)) * Math.cos(deg2rad(to.lat)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}
function deg2rad(deg)
{
return deg * (Math.PI/180);
}