JSFiddle - React, Tailwind, and code Playground
JavaScript
/** Converts numeric degrees to radians */
if (typeof(Number.prototype.toRad) === "undefined") {
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
}
navigator.geolocation.getCurrentPosition(function(position){
calcPosition(position.coords.latitude,position.coords.longitude)
});
function calcPosition(lat1, lon1){
var lat2 = 45.843295;
var lon2 = -87.020821; // 2 is location
var R = 3963.1676; // radius in mi
var x1 = lat2-lat1;
var dLat = x1.toRad();
var x2 = lon2-lon1;
var dLon = x2.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
alert("You are"+" "+d+" "+"miles away from our school")
}