0

I am trying to make an app that finds a user's location, and then alerts them how far they are from a fixed location. I am new to JavaScript, and I'm having a lot of trouble trying to figure it out. The part that I cannot figure out is how to get the user's location in for the variable lat1 and lon1. I have researched how to just figure out the user's longitude and latitude, but all I can find is stuff about the getCurrentPosition() command. The only problem with this is that I don't want the values returned when they are found. Is their anyway that I could somehow achieve this? My code is below:

var lat2 = 45.843295; 
var lon2 = -87.020821; // 2 is location
var lat1 = 
var lon1 =
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; 
var TellMe= function(){
alert("You are"+" "+d+" "+"miles away from our school")
};

lon1 and lat1 are supposed to be the user's location.

2 Answers2

1

What you want is to do your calculations in a callback function once navigator.getCurrentPosition() returns successfully. Furthermore, the toRad() function is not defined, so you'd need to define it first.

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")
}

Here is a fiddle.

EDIT: Here is an answer with a performance-optimized version of the formula.

Community
  • 1
  • 1
JstnPwll
  • 8,585
  • 2
  • 33
  • 56
0

Have you tried with the HTML5 Geolocation?? : Here is the simple Javascript by which you can get the location:

if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showLocation);
    } else { 
        x.innerHTML = "Geolocation is not supported.";
    }
}

function showLocation(position) {
    var lat1 = position.coords.latitude;
    var lon1 = position.coords.longitude;
    x.innerHTML = "Latitude: " + lat1   + 
    "<br>Longitude: " +  lon1 ;
}
Sandeep Kokate
  • 825
  • 4
  • 16
  • He already knows about `getCurrentPosition`, he is asking how to get the distances between 2 locations – Endless May 23 '16 at 16:49