ファクトリへの呼び出しが、マップ コントローラーですぐに返されます。マップの中心を設定しようとしていますが、geo の呼び出しが undefined を返すため、中心が設定されません。ただし、座標はジオ ファクトリで取得されます。彼らは遅すぎます。
.then() のポイントは、日付が返されるのを待つことだと思いました。では、アプリを強制的に待機させるにはどうすればよいでしょうか?
私の工場は:
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
};});
私のマップコントローラーは次のように呼び出します:
// GEOLOCATION
geo.getPosition().then( function (position) {
console.log(position);
$scope.center.lat = position.latitude;
$scope.center.lon = position.longitude;
});