patrick
patrick

Reputation: 43

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

I am currently in the starting phase of building an app via Ionic. Right now i want to implement the cordova geolocation in it. However this keeps giving an error when opening it. For testing purposes i use ionic serve and check it in 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');
}
})

Is there any place i have made a mistake which causes this problem?

Upvotes: 2

Views: 4280

Answers (3)

Basu Sharma
Basu Sharma

Reputation: 13

I believe the issue is with your ngCordova installation . do below steps -

1- install --------------> bower install ngCordova

2- include in index.html above cordova.js ------------------> script src="lib/ngCordova/dist/ng-cordova.js"

3- inject -------------> angular.module(starter, ['ngCordova'])

run IONIC serve and issue will be gone .. If it still shows error that /dist/ngCordova is not present then go to location manually where ng cordova is installed and copy it to the path e.g. - /lib/ngCordova/dist/...

it will definitely resolve your issue

Upvotes: 1

YSJ
YSJ

Reputation: 382

I think it's not referenced as $cordovaGeolocation in ionic. Try navigator.geolocation.getCurrentPosition instead?

Upvotes: 0

MarkR
MarkR

Reputation: 188

Ionic serve only emulates the application in the browser. You do not get access to the Cordova plugins within the browser.

To get the libraries included you need to run the app on the device after adding a specific platform depending on what device you have.

For iOS:

Ionic platform add ios

For android:

ionic platform add android

After the platforms are added you can build and run on device using the following command

For iOS:

ionic run iOS

For android:

ionic run android

Upvotes: 2

Related Questions