I am trying to use the Javascript Geolocation API to get the user's longitude and latitude. I would like to be able to get that information by calling the getCoords() function but am having a problem. This code:
function getCoords() {
var coords = 5;
var deferred = $.Deferred();
getPosition = function() {
navigator.geolocation.getCurrentPosition(
function(position){
deferred.resolve({
longitude: position.coords.longitude,
latitude: position.coords.latitude,
});
}, function(error) {
deferred.reject();
});
return deferred.promise();
}
$.when(getPosition())
.done( function(data) {
coords = data;
console.log(coords); // Statement A
});
console.log(coords) //Statement B
return coords;
}
console.log(getCoords().toString()); //Statement C
Outputs the following to the console
5 //Statement B
5 //Statement C
Object {longitude: 41.40338, latitude: 2.17403} //Statement A
Which makes sense because the geolocation API is asynchronous. If I change the second half of the code to:
$.when(getPosition())
.done( function(data) {
coords = data;
return coords;
});
}
I get the following error message:
Uncaught TypeError: Cannot call method 'toString' of undefined
(anonymous function)
All of the other examples I have found on the web have the $.when part of the code output to an alert
instead of a return
method. Anyone have any thoughts on how to make getCoords() return the correct object? Thanks!