0

What i want to do is determining the location of user and then based on his location doing a request to Yelp API.

function getLocation()
  {
  if (navigator.geolocation)
    {
    var y = navigator.geolocation.getCurrentPosition(showPosition);
    return y;
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
 var y = position.coords.latitude + 
  "," + position.coords.longitude;
  return y;
  }


function doRequest(){

var terms = 'turkish';
var near = 'San+Francisco';
var ll = getLocation();
... }

I have tested the variable y, it gives true latitude and longitude and if i write self a coordinate in ll, request code does work.

The problem is, in this situation, my ll parameter seems "undefined" in the request and i cant receive any response from API . I don't understand what the problem is. Any idea?

Asqan
  • 4,319
  • 11
  • 61
  • 100
  • Seems like the `getLocation()` function is not returning. Could it be that it goes through the `else` branch? – Aioros Dec 11 '13 at 16:18
  • No because i have tested the code. If it was so, i would not receive the coordinates. I didn't write my test codes to keep it simple to understand – Asqan Dec 11 '13 at 16:21
  • I'm not sure I'm understanding the situation. Is the `ll` variable undefined or not? And if not, where is the problem instead? – Aioros Dec 11 '13 at 16:23
  • 2
    getCurrentPosition is not returning anything as well as it is rather not calling showPosition instantly. it probably waits for location and then calls showPosition. Read about asynchronous programming, in JS most of the things is done this way. – lupatus Dec 11 '13 at 16:23
  • Well, lupatus is absolutely right. – Aioros Dec 11 '13 at 16:25
  • getCurrentPosition is asynchronous hence it doesn't return anything, you have to wait for the value somewhere else (like the callback showPosition and do something then) – Charlie Affumigato Dec 11 '13 at 16:26

1 Answers1

2

The Geolocation API is asynchronous, so you have to wait for the data to be returned

function getLocation(callback) {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            callback({
                success : true, 
                result  : position.coords.latitude + "," + position.coords.longitude
            });
        });
    } else {
        callback({
            success : false, 
            result  : "Geolocation is not supported by this browser."
        });
    }
}

function doRequest() {
    var terms = 'turkish';
    var near = 'San+Francisco';
    getLocation(function(result) {
        if (result.success) {
            var y = result.result;

            // do something with y here
        }else{
            x.innerHTML = result.result;
        }
    });
}
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • This is the point i had missed. Thanks! I have tried it but ll parameter that i need is still missed. `var ll = -1; getLocation(function(result) { if (result.success) { ll =result.result; ... ` and when i use ll in request, it is still undefined – Asqan Dec 11 '13 at 16:38
  • 1
    You can only use it inside the callback, the Geolocation is asynchronous, so it returns the coordinates at a later time, read this -> http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – adeneo Dec 11 '13 at 16:45