0

I am running following javascript as a test using jsfiddle.net -

var v1=1;
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};

function success(pos) {
  var crd = pos.coords;

  alert('Your current position is:');
  alert('Latitude : ' + crd.latitude);
  alert('Longitude: ' + crd.longitude);
  alert('More or less ' + crd.accuracy + ' meters.');
  v1=crd.latitude;
  alert(v1);
};

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

alert(v1); 
navigator.geolocation.getCurrentPosition(success, error, options);
alert(v1); //removing this makes the code work

The code works fine until the last alert is put in place. The success function isn't invoked in that case. I am baffled if it's a global variable declaration issue or the way getCurrentPosition is being invoked. All I want is to have the longitude and latitude values in a variable that I can use later. Newbie here. Any pointers?

  • 1
    The HTML5 Geolocation API is asynchronous, so that will never work. – adeneo May 09 '14 at 23:07
  • 2
    http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call?rq=1 , http://stackoverflow.com/questions/8187201/return-value-from-inside-of-ajax-function - different function, same problem – user2864740 May 09 '14 at 23:08

1 Answers1

0

Which Browser are you using? The above code works fine in chrome.

The second alert could be halting execution of your success function. better use console.log() for debugging.

321hendrik
  • 177
  • 1
  • 8