So I have a code here:
.factory("Geolocation", ["$cordovaGeolocation",
function ($cordovaGeolocation) {
var posOptions = {
timeout: 10000,
enableHighAccuracy: false
};
var getCurrentPosition = $cordovaGeolocation.getCurrentPosition(posOptions);
return {
lat: function () {
getCurrentPosition.then(function(position) {
return position.coords.latitude; //getting undefined
})
},
long: function() {
getCurrentPosition.then(function(position) {
return position.coords.longitude;
})
}
}
}
])
And in my controller I call it Geolocation.lat()
or Geolocation.long()
.
Now my problem is that I am getting undefined instead of the lat and long.
If I do something like:
return getCurrentPosition.then(function(position) {
return position.coords.longitude;
})
I am getting another promise which is redundant and what I only want to happen is to get the lat and long.
Is there a simple way to access the variables inside of that promise?
Any help would be much appreciated.
UPDATE (tried declaring a variable outside and to be filled inside the promise but I still get a null value.
.factory("Geolocation", ["$cordovaGeolocation", "$rootScope",
function ($cordovaGeolocation, $rootScope) {
var posOptions = {
timeout: 10000,
enableHighAccuracy: false
};
var getCurrentPosition = $cordovaGeolocation.getCurrentPosition(posOptions);
$rootScope.latitude = null;
$rootScope.longitude = null;
return {
lat: function () {
getCurrentPosition.then(function(position) {
$rootScope.latitude = position.coords.latitude;
})
return $rootScope.latitude;
},
long: function() {
getCurrentPosition.then(function(position) {
return $rootScope.longitude;
})
}
}
}
])