first post here! Trying to fix this and looking for an answer online but not luck -I know its out there, but cant seem to figure it out- I have a javascript code to return the location of the user as follows:
var myLatlon = navigator.geolocation.getCurrentPosition(onSuccess, onError);
And the function is as follows:
var onSuccess = function(position) {
var latlon=position.coords.latitude+','+position.coords.longitude;
return latlon;
};
When I do a console.log(latlon); inside the function above it returns the actual latitude and longitude separated by a comma.
But when I do a console.log(myLatlon) after the first line; it returns: {“timer”:true}
I need to return from my function the actual latitude and longitude. Any ideas?
You cannot return the value from
navigator.geolocation.getCurrentPosition. That’s the nature of asynchronous calls. The call tonavigator.geolocation.getCurrentPositionfinishes way before the callback is called. You have to use your coordinates within youronSuccesscallbackUsually restructuring your code has almost the same effect as returning it.