2

我基本上是在尝试使用地理位置 api检索用户的当前位置,然后将坐标作为参数传递给 angularjs ajax 调用并返回自定义“地址”对象,如下所示:

从使用的模块:

angular.module('geolocationModule', ['ng'])
.factory('geolocationService', ['$http', function geolocationService($http) {
    return {
        geolocationPosition : function(){
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    return $http.post('/bignibou/geolocation/approximateReverse/', {lat: position.coords.latitude, lng: position.coords.longitude})
                    .then(function(formattedAddress){
                        var address = {};
                        address.formattedAddress = formattedAddress;
                        return address;
                    },
                    function(data, status, headers, config){
                        console.log(data);
                        console.log(status);
                    });
                }, errorFn, {
                    timeout : 10000
                });
            }
        }
    };  
}]);

从调用模块:

angular.module('searchEngineModule', ['ng', 'geolocationModule'])
.controller('geolocationCtrl', [ '$scope', 'geolocationService', function geolocationCtrl($scope, geolocationService) {
        $scope.init = function(){
            var geolocationAddress ={};
            geolocationAddress = geolocationService.geolocationPosition();
            console.log(geolocationAddress);
        };
}]);

出于某种原因,我的 geolocationAddress 对象始终未定义...

有人可以帮忙吗?

编辑1

如果我使用以下异步代码:

var geolocationAddress ={};
geolocationService.geolocationPosition().then(function(data) {
     geolocationAddress = data;
});
console.log(geolocationAddress);

这是我得到的:

TypeError: Cannot call method 'then' of undefined
    at Object.$scope.init (http://localhost:8080/bignibou/js/custom/searchEngine.js:5:45)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:6541:19
    at Object.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:8218:28)
    at pre (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:13636:15)
    at nodeLinkFn (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:4568:13)
    at compositeLinkFn (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:4191:15)
    at publicLinkFn (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:4096:30)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:1042:27
    at Object.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:8218:28)
    at Object.Scope.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js:8298:23) <body ng-app="searchEngineModule" ng-controller="geolocationCtrl" ng-init="init()" style="" class="ng-scope"> 

编辑 2:考虑到 Chandermani 的评论并更新到以下内容,对问题进行了排序:

angular.module('geolocationModule', ['ng'])
.factory('geolocationService', ['$http', '$q', '$rootScope', function geolocationService($http, $q, $rootScope) {

    function geolocationPositionP (){
        var deferred = $q.defer();
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(                   
            function (position) {
                $rootScope.$apply(function(){
                    console.log(position);
                    deferred.resolve(position);
                });
            },
            function(error){
                deferred.reject(error);
            },
            {timeout: 10000, maximumAge: 0});
        }
        return deferred.promise;
    }

    function geolocationAddressP(){
        var deferred = $q.defer();
        var address = {};
        var geolocationPromise = geolocationPositionP();
        geolocationPromise.then(function(position){
            $http.post('/bignibou/geolocation/approximateReverse/', {lat: position.coords.latitude, lng: position.coords.longitude})
            .success(function(formattedAddress){
                address.formattedAddress = formattedAddress;
                address.latitude = position.coords.latitude;
                address.longitude = position.coords.longitude;
                deferred.resolve(address);
            })
            .error(function(data, status, headers, config){
                console.log(data);
                console.log(status);
                deferred.reject(data);
            });
        });
        return deferred.promise;
    }

    return {
        geolocationAddress : function(){
            return geolocationAddressP();
        } 
    };  
}])
4

1 回答 1

3

这似乎是一个异步调用问题。你能试一下吗

 geolocationService.geolocationPosition().then(function(data) {
     geolocationAddress = data;
});

更新:由于代码的异步性质,该方法geolocationPosition需要返回承诺或资源对象。或者你甚至可以尝试回调方法

 geolocationPosition : function(){
            var defer=$q.defer();
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                    return $http.post('/bignibou/geolocation/approximateReverse/', {lat: position.coords.latitude, lng: position.coords.longitude})
                    .then(function(formattedAddress){
                        var address = {};
                        address.formattedAddress = formattedAddress;
                        defer.resolve(address);
                    },
                    function(data, status, headers, config){
                        console.log(data);
                        console.log(status);
                        defer.reject(status)
                    });
                }, errorFn, {
                    timeout : 10000
                });
                return defer.promise
            }

阅读一些关于$q http://docs.angularjs.org/api/ng .$q

于 2013-11-12T15:45:06.557 回答