ionic.bundle.js:25642错误:[$injector:unp]未知提供程序:$cordovaGeoloc

ionic.bundle.js:25642 Error: [$injector:unpr] Unknown provider: $cordovaGeolocationProvider <- $cordovaGeolocation <- AgeCtrl

本文关键字:未知 cordovaGeoloc 程序 unp injector 25642 bundle 错误 ionic js      更新时间:2023-09-26

我目前正处于通过Ionic构建应用程序的开始阶段。现在我想在其中实现cordova地理定位。然而,打开它时会出现错误。出于测试目的,我使用ionic serve并在localhost中检查它。

angular.module('starter', ['ionic','ionic.service.core', 'ui.router'])
.controller('AgeCtrl', function ($scope, $state, $http, $cordovaGeolocation) {

$scope.toggleItem = function (item) {
    item.checked = !item.checked;
};
$scope.items = [
  { id: '0-12' },
  { id: '12-18' },
  { id: '18-30' },
  { id: '30-65' },
  { id: '65+' }
];
    $scope.time = Date.now();
   $scope.weather = $http.get("http://api.openweathermap.org/data/2.5/weather?q=Amsterdam&units=metric&APPID=...").then(function(resp) {
   console.log("success", resp);
   }, function(err) {
    console.log("error");
   })
var posOptions = {timeout: 10000, enableHighAccuracy: false};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
  var lat  = position.coords.latitude
  var long = position.coords.longitude
  console.log(lat + '   ' + long)
}, function(err) {
  console.log(err)
});
$scope.Confirm = function (){
    $state.go('home');
}
})

有没有什么地方我犯了错误导致了这个问题?

Ionic serve仅在浏览器中模拟应用程序。您无法访问浏览器中的Cordova插件。

要获得包含的库,您需要在添加特定平台后在设备上运行应用程序,具体取决于您拥有的设备。

适用于iOS:

Ionic平台添加ios

对于android:

离子平台添加安卓

添加平台后,您可以使用以下命令在设备上构建和运行

适用于iOS:

ionic运行iOS

对于android:

离子运行安卓

我认为问题出在ngCordova安装上。执行以下步骤-

1-安装--------------->bower安装ngCordova

2-包含在cordova.js上方的index.html中------------------>脚本src="lib/ngCordova/dist/ng cordova.js"

3-注入--------------->角模块(启动器,['ngCordova'])

运行IONIC服务,问题将消失。。如果仍然显示/dist/ngCordova不存在的错误,则手动转到安装ng cordova的位置,并将其复制到路径,例如-/lib/ngCor多瓦/dist/。。。

它肯定会解决您的问题

我认为它在ionic中没有被引用为$cordovaGeolocation。是否改为尝试navigator.geolocation.getCurrentPosition