AngularJs GPS 访问在其他州被拒绝
AngularJs GPS access denied in other state
我正在开发一个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);
}
请注意,此代码完全未经测试。我只是想把这个想法传达出去。
干杯!
相关文章:
- 如何添加浮动和非浮动,其他
- 与其他库的jQuery.noConflict()
- 播放当前视频时如何停止其他视频?JavaScript
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 制作一个不带HTML a标记但在动画播放完毕后指向其他页面的超链接
- 除修剪外的其他功能
- 在流星上使用微信js-sdk时出现拒绝权限错误
- 是什么让一个“;Uncaught RangeError:超过了最大调用堆栈大小“;错误(Chrome,在其他浏览器中显示
- Array.length似乎不起作用;console.log则显示其他情况
- 如何消除代码中的未定义和其他问题
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- jquery代码在Mozilla中有效,但在其他浏览器上无效
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- Jquery未定义函数正在停止其他操作
- 如何将我的javascript库公开给其他客户端使用
- Javascript阻止其他Javascript代码
- 拒绝与.htaccess或其他任何内容的主链接
- AngularJs GPS 访问在其他州被拒绝
- 折线拒绝显示在IE8上,在其他地方工作
- 被 Mozilla 接受的 XMLHTTPRequest 在其他浏览器中被拒绝