My call to the factory returns too quickly in my map controller. I am trying to set the centre of the map however since the call to geo returns undefined it never sets centre. However the coordinates are grabbed in the geo factory. They are just too late.
I thought the point of .then() was to wait for the date to be returned. So, how do I force my app to wait?
My factory is:
angular.module('comhubApp')
.factory('geo', function ($q) {
var getPosition = function () {
var deferred = $q.defer();
if (navigator.geolocation) {
deferred.resolve(navigator.geolocation.getCurrentPosition(function (position) {
var crd = position.coords;
console.log('Latitude : ' + crd.latitude);
console.log('Longitude: ' + crd.longitude);
return crd;
} ));
}
return deferred.promise;
}
// Public API here
return {
getPosition: getPosition
};});
My map controller calls it with:
// GEOLOCATION
geo.getPosition().then( function (position) {
console.log(position);
$scope.center.lat = position.latitude;
$scope.center.lon = position.longitude;
});