Reputation: 1941
I have this function that is trying to return the latitude and longitude as a string when called. However when i call it with an alert it returns undefined. But when I alert the data.coords.latitude/longitude it gives me the correct values. Any help is greatly appreciated.
function GetLocation() {
var jsonLocation;
navigator.geolocation.getCurrentPosition(function (data) {
jsonLocation = data.coords.latitude+','+data.coords.longitude;
});
return String(jsonLocation);
}
alert(GetLocation());
Upvotes: 2
Views: 3681
Reputation: 664528
getCurrentPosition()
expects a callback:
function getLocation(callback) {
navigator.geolocation.getCurrentPosition(function (data) {
var jsonLocation = data.coords.latitude+','+data.coords.longitude;
callback(jsonLocation);
});
}
getLocation(alert);
Upvotes: 4
Reputation: 1941
I changed up the code so the callback would alert the information when the getCurrentPosition returns
function RequestLocation() {
navigator.geolocation.getCurrentPosition(function (data) {
SendLocation(String(data.coords.latitude + ',' + data.coords.longitude));
});
}
function SendLocation(cords)
{
alert(cords);
}
RequestLocation();
Upvotes: 0