-1

I'm trying to create a simple function to return user location based on browser but it keeps returning undefined:

  function getLocation() {
    //get current location
    var geo_param;
    if(navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        // Create geolocation parameter
        geo_param = "geolocation=[" + position.coords.latitude + "," + position.coords.longitude + "]";
        console.log(geo_param)
      });
    }
    return geo_param
  }

  function search(activity) {
    var searchUrl = "/s?activity=" + activity + "&" + getLocation()
    window.location.href = searchUrl;
    console.info(searchUrl)
  }
Batman
  • 5,563
  • 18
  • 79
  • 155

2 Answers2

1

The getCurrentPosition scope is asynchronous, so geo_param will be returned before getCurrentPosition finished runnning. An solution to this is using a callback, see my example below.

 function getLocation(callback, activity) {
     //get current location
     var geo_param;
     if (navigator.geolocation) {
         navigator.geolocation.getCurrentPosition(function (position) {
             // Create geolocation parameter
             geo_param = "geolocation=[" + position.coords.latitude + "," + position.coords.longitude + "]";
             console.log(geo_param);
             callback(geo_param, activity);
         });
     }
 }

 function search(activity) {
     getLocation(search_with_current_location, activity);
 }

 function search_with_current_location(geo_param, activity) {
     var searchUrl = "/s?activity=" + activity + "&" + geo_param;
     window.location.href = searchUrl;
     console.info(searchUrl);
 }
Rayon
  • 36,219
  • 4
  • 49
  • 76
maxdaniel98
  • 6,623
  • 6
  • 19
  • 21
0

In this case, your function executes before the response is being fetched. You need to have a callback function once you get the response from the API.

Also note you did not have query string key geo_param in redirection URL

Try this:

function getLocation(cb) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
            var geo_param = "geolocation=[" + position.coords.latitude + "," + position.coords.longitude + "]";
            if (typeof cb === 'function') {
                cb(geo_param);
            }
        });
    }
}
function search(activity) {
    getLocation(function (geo_param) {
        window.location.href = "/s?activity=" + activity + "&" + geo_param;
    });
}
Rayon
  • 36,219
  • 4
  • 49
  • 76
  • Hey, thanks for answer. I do have the query string key however, it's called geolocation. So it looks like '&geolocation=[lon,lat]` the way you have it would return `&geo_param=geolocation=[lon,lat]` – Batman Sep 29 '15 at 06:57
  • 1
    @Batman, Ooh yes! I got that wrong.. – Rayon Sep 29 '15 at 07:00
  • 1
    But yea it works perfectly. Thanks a lot, I think I'm getting it now. So in order to save the result of the async function, I need to pass it to another function and work with it from there. – Batman Sep 29 '15 at 07:05
  • 1
    Yeah exactly! For `async` operations as functions are first-class objects, we can pass a function as an argument in another function and later execute that passed-in function or even return it to be executed later. – Rayon Sep 29 '15 at 07:07
  • 1
    Ah okay, I get it, a little confusing but I understand. Thanks for the help – Batman Sep 29 '15 at 07:11