如何在angularjs中使用geolocation watchPosition?

时间:2014-10-11 13:52:08

标签: angularjs geolocation

我想在角度js中使用geolocation.watchPosition,以便每3分钟找到一个类似gps跟踪器的新位置。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

appControllers.controller('Ctrl', ['$scope','$routeParams', '$http','$window','$interval',
function geo_success(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
console.log(lat);
console.log(lng);
$scope.$apply(function(){
$scope.lat = lat;
$scope.lng = lng;
});
$scope.latlng = function(){
$http.post('/profile', {
latitude: $scope.lat,
longitude: $scope.lng
})
.success(function(user){
// No error: authentication OK
$rootScope.message = 'Lat/Lng send ok!';
})
.error(function(){
// Error: authentication failed
$rootScope.message = 'Lat/Lng dont send';
});
};
}
function geo_error(error){
console.log ("gps lost");
}
var watchID = $window.navigator.geolocation.watchPosition(geo_success,geo_error,     {enableHighAccuracy: true });
}]);

this i have write 

答案 1 :(得分:0)

希望这可以提供帮助。

angular.module("GeoService",[]).
factory('GeoLocation',['$rootScope', '$window', '$q', '$resource', function($rootScope, $window, $q, $resource){
    var _geoCapable = false,
        _deferred = $q.defer(),
        _errorMsg = null;
        //Geolocation options
        _option = {
                enableHighAccuracy:true,
                timeout: 5000,
                maximumAge: 0,
                desiredAccuracy: 0,
                frequency: 1         
            };

    if($window.navigator && $window.navigator.geolocation){
        _geoCapable = true;
    }

    if(!_geoCapable){
        $rootScope.$broadcast('geo:error', 'Geolocation not supported');
    }

    //Geolocation function onSucces
    _onSucces = function (position) {
                _deferred.resolve(position);               
            };

    //Geolocation function onFail
    _onFail =  function (error) {
                switch (error.code) {
                    case error.PERMISSION_DENIED:
                    _errorMsg = "User denied the request for Geolocation."
                    break;
                    case error.POSITION_UNAVAILABLE:
                    _errorMsg = "Location information is unavailable."
                    break;
                    case error.TIMEOUT:
                    _errorMsg = "The request to get user location timed out."
                    break;
                    case error.UNKNOWN_ERROR:
                    _errorMsg = "An unknown error occurred."
                    break;
                };
                $rootScope.$broadcast('geo:error', _errorMsg);
                _deferred.reject(_errorMsg);
            };

    //Service API
    var _locationService = {
        //getPosition() function:
        getLocation: function(){
            if(!_geoCapable){
                return _deferred.reject();
            };
            $window.navigator.geolocation.getCurrentPosition(_onSucces,_onFail,_option);
            return _deferred.promise;
        },

        //watchPosition() function:
        watchPosition: function(){
            if(!_geoCapable){
                $rootScope.$broadcast('geo:error','Geolocation not supported');                
            };
            $window.navigator.geolocation.watchPosition(function(position){
                $rootScope.$broadcast('geo:positionUpdate', position);
            }, _onFail);
        }
    };
    return _locationService;
}]);