AngularJs GPS 访问在其他州被拒绝

AngularJs GPS access denied in other state

本文关键字:拒绝 其他 GPS 访问 AngularJs      更新时间:2023-09-26

我正在开发一个Cordova应用程序,我使用不同的AngularJs状态。如果我在第一个状态 geolocation.watchposition 中调用它,它工作得很好,但如果我在第二个状态下调用它,我的访问被拒绝......

我用按钮更改状态。不管,我先从哪个州开始,第一个有GPS,第二个没有。

编辑:我应该提到,它在浏览器中工作,但不能在我的安卓设备上工作。

你知道为什么吗?

索引.js

.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
    .state('app', {
      url: '/app',
      templateUrl: 'templates/main_menu.html',
      controller: 'AppCtrl'
    })
   .state('map', {
       url: '/map',
       templateUrl: 'templates/map.html',
       controller: 'MapCtrl'
   });
   //First State
   $urlRouterProvider.otherwise('/app');
});

控制器.js

.controller('AppCtrl', function ($scope, $rootScope, $ionicHistory, $http, $window) { 
  $scope.accPos = function () {
      var id, target, options;
      function success(pos) {
          alert("Pos: " + pos.coords.latitude + " " + pos.coords.longitude);
      }
      function error(err) {
          alert('ERROR(' + err.code + '): ' + err.message);
      }
      options = {
          enableHighAccuracy: false,
          timeout: 6000,
          maximumAge: 0
      };
      id = navigator.geolocation.watchPosition(success, error, options);
   };
   $scope.accPos();
}
//Looks exactly the same
.controller('MapCtrl', function ($scope, $rootScope, $ionicHistory, $http, $window) { ... }

您确实应该将此类代码移动到服务中,以便可以在控制器之间共享它。最重要的是,您可以利用ui路由器的解析功能来解析需要它的每个州的GPS位置。

例如:

服务.js

.factory('GeoLocationService', function ($window) { 
    var id, target, options, lastPosition;
    options = {
        enableHighAccuracy: false,
        timeout: 6000,
        maximumAge: 0
    };
    var geoLocationService = {
        startWatching: startWatching,
        stopWatching: stopWatching,
        getLastPosition: getLastPosition,
        options: options
    };  
    startWatching();
    return geoLocationService;
    function getLastPosition() {
        return lastPosition;
    }
    function startWatching() {
        id = $window.navigator.geolocation.watchPosition(success, error, options);
    }
    function stopWatching() {
        $window.navigator.geolocation.clearWatch(id);
    }
    function success(pos) {
        lastPosition = pos;
        alert("Pos: " + pos.coords.latitude + " " + pos.coords.longitude);
    }
    function error(err) {
        alert('ERROR(' + err.code + '): ' + err.message);
    }
});

索引.js

.config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
    .state('app', {
      url: '/app',
      templateUrl: 'templates/main_menu.html',
      controller: 'AppCtrl',
      resolve: {
         location: function(GeoLocationService){
            return GeoLocationService.getLastPosition();
         }
      }
    })
   .state('map', {
       url: '/map',
       templateUrl: 'templates/map.html',
       controller: 'MapCtrl',
       resolve: {
         location: function(GeoLocationService){
            return GeoLocationService.getLastPosition();
         }
      }
   });
   //First State
   $urlRouterProvider.otherwise('/app');
});

控制器.js

.controller('AppCtrl', function ($scope, $rootScope, $ionicHistory, $http, $window, GeoLocationService, location) {
    // show the location at start
    alert("Pos: " + location.coords.latitude + " " + location.coords.longitude);
    // maybe watch the location from the service
    $scope.$watch(function () {
        return GeoLocationService.getLastPosition();
    },
    function (newValue, oldValue) {
        if (newValue !== oldValue) {
            // do something
        }            
    }, true);
}

请注意,此代码完全未经测试。我只是想把这个想法传达出去。

干杯!