Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在angularjs中使用地理定位watchPosition?_Angularjs_Geolocation - Fatal编程技术网

如何在angularjs中使用地理定位watchPosition?

如何在angularjs中使用地理定位watchPosition?,angularjs,geolocation,Angularjs,Geolocation,我想使用angular js中的geolocation.watchPosition,以便每隔3分钟找到我的新位置,比如gps跟踪器。有人能帮我吗?希望这能帮上忙 appControllers.controller('Ctrl', ['$scope','$routeParams', '$http','$window','$interval', function geo_success(position) { var lat = position.coords.latitude; var lng =

我想使用angular js中的geolocation.watchPosition,以便每隔3分钟找到我的新位置,比如gps跟踪器。有人能帮我吗?

希望这能帮上忙

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 
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;
}]);

如果您只想每三分钟获得一次位置,那么最好使用Geolocation.getCurrentPosition。Geolocation.watchPosition用于在设备位置每次更改时自动获取更新。我认为最好在设备位置每次更改时获取位置。。所以我想我必须使用geolocation.watchPosition。你知道我如何在angular中使用它吗?你在问题中写道,你想每三分钟找到一次位置。您想在每次设备位置更改时获取位置吗?你试过什么了吗?你能显示任何代码吗?是的,最后最好是每次设备的位置发生变化时都记录位置。我在一个控制器中写了这个。。函数geo_successposition{var lat=position.coords.lation;var lng=position.coords.longitude;console.loglat;console.loglng;$scope.$applyfunction{$scope.lat=lat;$scope.lng=lng;};函数geo_errorerror{console.log gps lost;}var watchID=$window.navigator.geolocation.watchPositiongeo_成功,geo_错误,{enableHighAccurance:true};