Angularjs 基于离子骨架的ngCordova地理定位
我正在尝试将ngCordova地理定位与我的ionic应用程序结合使用。我做了以下工作: 1) Bower已安装并在index.html中包含ngcordova.min.js 2) 在我的angular app.module中将“ngCordova”作为依赖项注入Angularjs 基于离子骨架的ngCordova地理定位,angularjs,geolocation,ionic,ngcordova,Angularjs,Geolocation,Ionic,Ngcordova,我正在尝试将ngCordova地理定位与我的ionic应用程序结合使用。我做了以下工作: 1) Bower已安装并在index.html中包含ngcordova.min.js 2) 在我的angular app.module中将“ngCordova”作为依赖项注入 angular .module('app', ['ionic', 'ngCordova']) .run(runConfig); 3) 在我的控制器中添加了依赖项 SomeController.$inject = ['$locatio
angular
.module('app', ['ionic', 'ngCordova'])
.run(runConfig);
3) 在我的控制器中添加了依赖项
SomeController.$inject = ['$location', '$cordovaGeolocation',
'$ionicPlatform']
4) 这就是我的控制器的外观:
(function() {
'use strict';
angular
.module('app.submission')
.controller('SomeController', SomeController);
SomeController.$inject = ['$location', '$cordovaGeolocation', '$ionicPlatform'];
function SomeController($location, $element, $cordovaGeolocation, $ionicPlatform) {
var vm = this;
vm.place = '';
vm.address = {
'latitude' : null,
'longitude' : null
}
vm.initialize = initialize;
vm.getAddress = getAddress;
function initialize() {
var input = (document.getElementById('location-search'));
var searchBox = new google.maps.places.SearchBox((input));
}
function getAddress(){
var posOptions = {timeout: 20000, enableHighAccuracy: true};
$cordovaGeolocation.getCurrentPosition(posOptions)
.then(function(position){
var lat = position.coords.latitude
var long = position.coords.longitude
console.log('lat', lat);
console.log('long', long);
}, function(error){
console.log('error:', error);
});
}
}
})();
5) 单击按钮时调用函数getAddress()
当我在web浏览器(google chrome)上测试应用程序时,我得到以下错误
TypeError: $cordovaGeolocation.getCurrentPosition is not a function
at SomeController.getAddress (SomeController.js:29)
at $parseFunctionCall (ionic.bundle.js:21044)
at ionic.bundle.js:53458
at Scope.$eval (ionic.bundle.js:23100)
at Scope.$apply (ionic.bundle.js:23199)
at HTMLButtonElement.<anonymous> (ionic.bundle.js:53457)
at HTMLButtonElement.eventHandler (ionic.bundle.js:11713)
at triggerMouseEvent (ionic.bundle.js:2863)
at tapClick (ionic.bundle.js:2852)
at HTMLDocument.tapMouseUp (ionic.bundle.js:2925)
TypeError:$cordovaGeolocation.getCurrentPosition不是函数
位于SomeController.getAddress(SomeController.js:29)
at$parseFunctionCall(ionic.bundle.js:21044)
在爱奥尼亚.邦德.js:53458
范围$eval(IONAL.bundle.js:23100)
在范围内$apply(IONAL.bundle.js:23199)
在HTMLButtoneElement。(ionic.bundle.js:53457)
在HTMLButtonElement.eventHandler(ionic.bundle.js:11713)
在triggerMouseEvent上(ionic.bundle.js:2863)
在点击时(ionic.bundle.js:2852)
在HTMLDocument.tapMouseUp(ionic.bundle.js:2925)
有人能帮我一下吗?我的代码怎么了?它应该只在移动设备上工作吗?我在安卓设备上试过,但没有成功。我使用的是相同的代码,请尝试用这些代码替换
SomeController.$inject = ['$scope','$location', '$cordovaGeolocation', '$ionicPlatform'];
function SomeController($scope,$location, $element, $cordovaGeolocation, $ionicPlatform) {
var posOptions = {timeout: 20000, enableHighAccuracy: true,maximumAge: 0 };
$scope.where = function(){
$cordovaGeolocation.getCurrentPosition(posOptions)
.then(function(position){
console.log(position);
$scope.lat1 = position.coords.latitude
$scope.lng1 = position.coords.longitude
}, function(error){
console.log('error:', error);
});
};
}
如果这有帮助,请张贴整个控制器或至少更大的一部分。可能存在
$inject
不正常工作的依赖性问题。尝试注释整个函数,只需执行一个alert或console.log/$log.debug,查看控制器中是否显示任何内容。我注意到一个丢失的代码>在var posOptions={timeout…
行的末尾。使用完整的控制器代码更新了我的问题谢谢您的回答。它在浏览器中工作,但会返回错误调用并抛出positionError。对于设备,请不要使用.min.js