0

I need to get the latitude and longitude coordinates of a user in a webrowser to calculate the distance between the user and various restaurants. to do this, first I need the latitude and longitude,

I've been using this code to get lat and long, but i cant seem to get the actaul values saved to do calculations, and it seems quite a few people have had this issue too, and after doing a lot of searching, I still have not found a solution.

    <script type="text/javascript">

var jArray= <?php echo json_encode($restaurant); ?>;


//global variable
var userLat;
var userLng;
var acc;

if(!navigator.geolocation, jArray){
alert('Your Browser does not support HTML5 Geo Location. Please Use Newer Version Browsers');
}

navigator.geolocation.getCurrentPosition(success, error, jArray);
console.log(jArray);

function success(position, jArray)
{
var latitude = position.coords.latitude; 
var longitude = position.coords.longitude; 
var accuracy = position.coords.accuracy;
document.getElementById("lat").value = latitude;
document.getElementById("lng").value = longitude;
document.getElementById("acc").value = accuracy;

//updating global variable with user's latitude longitude
userLat = latitude;
userLng = longitude;
acc = accuracy;
//console.log(userLat)
console.log(jArray);

//var ulat = $('#lat').val();
//var ulongi = $('#lng').val();

//call your distance function here i.e after success, then only there will be values.
//distance(ulat, userLng ,28.459768, 77.05169939999999,"K");

}
function error(err){
alert('ERROR(' + err.code + '): ' + err.message);
}

function distance(lat1, lon1, lat2, lon2, unit)
{
var radlat1 = Math.PI * lat1/180
var radlat2 = Math.PI * lat2/180
var radlon1 = Math.PI * lon1/180
var radlon2 = Math.PI * lon2/180
var theta = lon1-lon2
var radtheta = Math.PI * theta/180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist)
dist = dist * 180/Math.PI
dist = dist * 60 * 1.1515
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
console.log(dist)
return dist
}


</script>

let me know how i can store the latitude and longitude from the browser globally, Thanks

Hannah
  • 23
  • 4
  • fetch the geolocation of user within the distance function oonly – jsjunkie Dec 01 '14 at 07:16
  • @jsjunkie may you provide more information please and possible code? – Hannah Dec 01 '14 at 07:27
  • I have tried that too and the value does not return – Hannah Dec 01 '14 at 07:57
  • The first issue is that geolocation is [asynchronous](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron), jsjunkie's answer addresses that. How are you calling the distance function? As an aside, do you really want the straight line (as the crow flies) distance? Or do you want the driving distance? – geocodezip Dec 01 '14 at 14:25
  • Hi geocodezip, I edited the code above any distance is fine, driving would be preferred, but is not necessary, I can call the distance function inside the geolocation function, the trouble I am having is passing my array with latitudes and longitudes into the geolocation function so that i can use it to calculate distances with that information, and save those distances in that array. – Hannah Dec 02 '14 at 02:18

1 Answers1

0

Ok, So I assume that in your function distance lat1 is user's latitude and lon1 is user's longitude.

Easiest thing we can do with your code is to store lat long in global variable. This is done by declaring variable outside all the function withing your script tag.

Below is the code sample. Try to figure out how things are executing.

//global variable
    var userLat;
    var userLng;
    var acc;

    if(!navigator.geolocation){
        alert('Your Browser does not support HTML5 Geo Location. Please Use Newer Version Browsers');
    }
    navigator.geolocation.getCurrentPosition(success, error);
    function success(position){
        var latitude  = position.coords.latitude;   
        var longitude = position.coords.longitude;  
        var accuracy  = position.coords.accuracy;
        document.getElementById("lat").value  = latitude;
        document.getElementById("lng").value  = longitude;
        document.getElementById("acc").value  = accuracy;

//updating global variable with user's latitude longitude
        userLat = latitude;
        userLng  = longitude;
        acc = accuracy;
    //call your distance function here i.e after success, then only there will be values.
     distance(28.459768, 77.05169939999999);
    }
    function error(err){
        alert('ERROR(' + err.code + '): ' + err.message);
    }


    //this code will be used to calculate distance between user and restaurants. I have restaurant latitude and longitude in a javascript array and have 227 instances
        function distance(lat2, lon2, unit) {
//making use of global variable
        var radlat1 = Math.PI * userLat/180
        var radlat2 = Math.PI * lat2/180
        var radlon1 = Math.PI * userLng/180
        var radlon2 = Math.PI * lon2/180
        var theta = userLng-lon2
        var radtheta = Math.PI * theta/180
        var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
        dist = Math.acos(dist)
        dist = dist * 180/Math.PI
        dist = dist * 60 * 1.1515
        if (unit=="K") { dist = dist * 1.609344 }
        if (unit=="N") { dist = dist * 0.8684 }
        return dist
    }
jsjunkie
  • 559
  • 3
  • 7
  • I have tried that and the variable comes back undefined when trying to console.log(userLat) outside the function – Hannah Dec 01 '14 at 07:56
  • could you please post the full code wrapped in html, or share jsfiddle link. I guess you must be invoking your function in a wrong way. I have made some edit in the post above. Check if that helps – jsjunkie Dec 01 '14 at 08:22
  • i already tried that, and it's close, but i need to do calculations using an array how do you pass an array into the function? – Hannah Dec 02 '14 at 00:07
  • It's a javascript question then. I guess you are writing incorrect method signature. In javascript you need not to add [] in method signature. So your method signature would remain as it is. You may simply iterate over lat2, lon2 if you pass arrays while calling it. – jsjunkie Dec 02 '14 at 04:45