0

I want to access(or print) d from return of function getDistanceFromLatLonInKm just by calling get.Location .I guess I may use callbacks but it dosent't work(and/or I'm a n00b).Is it possible to do this in this case? For example :document.write(getLocation(45.123,12.123)) //would print d somehow .

Thank you in advance.

getLocation(45.123,12.123);

function getLocation(a,b) {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(p){ajmo(p,a,b);});
  }
}

function ajmo(position,a,b) {
  lat = position.coords.latitude;
  lng = position.coords.longitude;
  getDistanceFromLatLonInKm(a,b,lat, lng);
}


function getDistanceFromLatLonInKm(lat_origin, lon_origin, lat_pos, lon_pos) {
  var R = 6371;
  var dLat = deg2rad(lat_pos - lat_origin);
  var dLon = deg2rad(lon_pos - lon_origin);
  var a = 
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(deg2rad(lat_origin)) * Math.cos(deg2rad(lat_pos)) * 
    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)
}     
4
  • You don't return values from asynchronous code as you're working in CPS, you need callbacks, or promises. Commented May 6, 2014 at 22:15
  • Yup, return is crucial here and it's missing in first two functions. Commented May 6, 2014 at 22:17
  • @elclanrs any idea how to implement it?(callbacks). Commented May 6, 2014 at 22:25
  • possible duplicate of Javascript: access return of the nested function -- please don't ask questions twice (or thrice). Commented May 6, 2014 at 22:55

1 Answer 1

1

Assuming you have an HTML element with id = "distance", you probably need this:

function getLocation(a, b, element) {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function (position) {
      lat = position.coords.latitude;
      lng = position.coords.longitude;
      var d = getDistanceFromLatLonInKm(a,b,lat, lng);
      document.getElementById(element).innerHTML = d;
    });
  }
}


function getDistanceFromLatLonInKm(lat_origin, lon_origin, lat_pos, lon_pos) {
  var R = 6371;
  var dLat = deg2rad(lat_pos - lat_origin);
  var dLon = deg2rad(lon_pos - lon_origin);
  var a = 
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(deg2rad(lat_origin)) * Math.cos(deg2rad(lat_pos)) * 
    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)
}  


getLocation(45.123, 12.123, 'distance');
Sign up to request clarification or add additional context in comments.

7 Comments

Since navigator.geolocation.getCurrentPosition is asynchronous, getLocation function cannot return any outcome from the asynchronous operation.
So,it is impossible to write 'd' with document.write(getLocation(a,b));?
If you want to set 'd' to the outcome value, then you should declare it outside getDistanceFromLatLonInKm function. But, since d will be assigned asynchronously, it's better to think what you want to do with this value, rather than just get the value. If you want to print it, then above code is enough. If you want to access it, call the function you want after it is assigned.
I just want to print it(d) in html only by calling getLocation.
You are actually do that :) Just replace console.log with code for setting HTML to an element. If you want, you can even pass the element id or class to getLocation and set the HTML of a specific element. When working with asynchronous programming, you have to swift your mind from the notion of having a function return a value.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.