1
votes

This function is asynchronous which is most of my problem in this case.

I want to get the current longitude and latitude of my location so I can then use these in the distanceFromCurrent function to calculate the distance between my current location and a given georss point.

Everything works fine except for the fact I can't use the currLat and currLong outside of its asynchronous function.

function getCurrentPosition(){
    navigator.geolocation.getCurrentPosition(getCoords, getError);
}

function getCoords(position){
    var currLat = position.coords.latitude;
    var currLon = position.coords.longitude;
}

function getError(error) {
    alert("Error");
}

// convert degrees to radians
Number.prototype.toRad = function() 
{ 
    return this * Math.PI / 180;
}

This is the function that calculates the distance from the georss and the current latitude and longitude it works fine with a set lat/lng like it has at the moment.

function distanceFromCurrent(georss) 
{  
    getCurrentPosition();
    var currLat = 3.0;
    var currLon = 4.0;

    georss = jQuery.trim(georss);
    var pointLatLon = georss.split(" ");
    var pointLat = parseFloat(pointLatLon[0]);
    var pointLon = parseFloat(pointLatLon[1]);

    var R = 6371;                   //Radius of the earth in Km             
    var dLat = (pointLat - currLat).toRad();    //delta (difference between) latitude in radians
    var dLon = (pointLon - currLon).toRad();    //delta (difference between) longitude in radians

    currLat = currLat.toRad();          //conversion to radians
    pointLat = pointLat.toRad();

    var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(currLat) * Math.cos(pointLat);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));   //must use atan2 as simple arctan cannot differentiate 1/1 and -1/-1
    var distance = R * c;   //sets the distance

    distance = Math.round(distance*10)/10;      //rounds number to closest 0.1 km
    return distance;    //returns the distance
}

So, does anyone have an ideas/solution to maybe getting that lat/lng a different way or am I going about this completely wrong?

3
did you find a solution? put your function in the callback, it's unfortunately how javascript worksuser1125394

3 Answers

0
votes

If the following call is none blocking then this won't work at all.

navigator.geolocation.getCurrentPosition(getCoords, getError);

The way your calling this means that the result of getCurrentPosition() won't have completed before your reached the end of the distanceFromCurrent() method.

Instead you could do the following and call update() instead of distanceFromCurrent(). This will ultimately call your distanceFromCurrent()method with the current lat/lng values.

Note: The following is psuedo code, i doubt it will correctly pass your georss value.

update()
{
 getCurrentPosition();
}
function getCurrentPosition(){
 navigator.geolocation.getCurrentPosition(getCoords, getError);
}

function getCoords(position){
 var currLat = position.coords.latitude;
 var currLon = position.coords.longitude;
 distanceFromCurrent(georss, lat, lng);
}

function distanceFromCurrent(georss, lat, lng) 
{  
    // Remove -> getCurrentPosition();
    var currLat = lat;
    var currLon = lng;

    georss = jQuery.trim(georss);
    ...
}
0
votes

add arguments in your distanceFromCurrent function, and call it inyour callback where georss is a global variable:

var georss = "43.12 7.12";

function getCoords(position){
    var currLat = position.coords.latitude;
    var currLon = position.coords.longitude;
    var distance = distanceFromCurrent(lat, lon, georss);
}

and

function distanceFromCurrent(georss, currLat, currLon){  
    // getCurrentPosition();
    // var currLat = 3.0;
    // var currLon = 4.0;
    georss = jQuery.trim(georss);
    var pointLatLon = georss.split(" ");
//....
-1
votes

getCorrentPosition is asynchronous. You need to send, as the 1st parameter a pointer to a funcion that will work with the data. Example

navigator.geolocation.getCurrentPosition(function(position) {
  var latitude = position.coords.latitude
  var longitude =  position.coords.longitude;
  distanceFromCurrent(currLatitude, currLongitude, latitude, longitude);
});

This should be a good way for you to start.

The problem is that you are trying to solve your problem in the wrong way. You are supposed to call getCurrentPosition and take care of all the returned data inside it. Maybe something like this (with some changes to your functions) do the trick.

Keep in mind that javascript never sleeps (does not have a sleep) so all that cannot be done "now" needs to be postponed. That's what happens with this method. You need to think about sending callbacks to functions instead of returning. Because I don't have all your code I don't know how to transform it. But basically you need:

function distanceFromCurrent(georss, callback) 
{ //...

you may look here for complete information: https://developer.mozilla.org/en-US/docs/Using_geolocation