$cordovaGeolocation not working inside ionic.Platform.ready #1182
Description
Am new to Ionic app dev. and i just run into error when trying to deploy this app into my android emulator and my android device but on my browser everything work perfectly well.
this is my app.js
var app = angular.module('caffeinehit', [
'ionic',
'ngCordova',
'caffeinehit.controllers',
'caffeinehit.services',
'caffeinehit.filters'
]);
app.run(function ($ionicPlatform) {
$ionicPlatform.ready(function () {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
});
app.config(function ($httpProvider) {
$httpProvider.defaults.headers.common['Authorization'] = 'Token eb2b5a2f205a493f2340889c5c8acdf520941f03';
});
this is my service.js
var app = angular.module('caffeinehit.services', []);
app.service("YelpService", function ($q, $http, $cordovaGeolocation, $ionicPopup) {
var self = {
'page': 1,
'isLoading': false,
'hasMore': true,
'results': [],
'lat': 51.544440,
'lon': -0.022974,
'refresh': function () {
self.page = 1;
self.isLoading = false;
self.hasMore = true;
self.results = [];
return self.load();
},
'next': function () {
self.page += 1;
return self.load();
},
'load': function () {
self.isLoading = true;
var deferred = $q.defer();
ionic.Platform.ready(function(){
$cordovaGeolocation
.getCurrentPosition({timeout:10000, enableHighAccuracy:false})
.then(function (position){
//self.lat =position.coords.latitude;
//self.lon =position.coords.longitude;
var params = {
page: self.page,
lat: self.lat,
lon: self.lon
};
$http.get('https://codecraftpro.com/api/samples/v1/coffee/', {params: params})
.success(function (data) {
self.isLoading = false;
console.log(data);
if (data.businesses.length == 0) {
self.hasMore = false;
} else {
angular.forEach(data.businesses, function (business) {
self.results.push(business);
});
}
deferred.resolve();
})
.error(function (data, status, headers, config) {
self.isLoading = false;
deferred.reject(data);
});
}, function(err){
console.error("Error Getting Position");
console.error(err);
$ionicPopup.alert({
title:self.lat,
template:self.lon
});
});
});
return deferred.promise;
}
};
self.load();
return self;
});
this is my controller.js
var app = angular.module('caffeinehit.controllers', []);
app.controller("YelpController", function ($scope, YelpService) {
$scope.yelp = YelpService;
$scope.doRefresh =function(){
if(!$scope.yelp.isLoading){
$scope.yelp.refresh().then(function(){
$scope.$broadcast('scroll.refreshComplete');
});
}
};
$scope.loadMore =function(){
console.log("load More");
if(!$scope.yelp.isLoading && $scope.yelp.hasMore){
$scope.yelp.next().then(function(){
$scope.$broadcast('scroll.infiniteScrollComplete');
});
}
};
$scope.getDirections =function(cafe){
console.log('get Direction from cafe');
var destination =[
cafe.location.coordinate.latitude,
cafe.location.coordinate.longitude
];
var source =[
$scope.yelp.lat,
$scope.yelp.lon
];
launchnavigator.navigate(destination, source);
};
});
this is my index.html
<title ></title ><link href="lib/ionic/css/ionic.css"
rel="stylesheet" >
<link href="css/style.css"
rel="stylesheet" >
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js" ></script >
<!-- cordova script (this will be a 404 during development) -->
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js" ></script >
<!-- your apps js -->
<script src="js/app.js" ></script >
<script src="js/controllers.js" ></script >
<script src="js/services.js" ></script >
<script src="js/filters.js" ></script >
CaffeineHit
{{cafe.name}}
<p >
<span >
<i class="ion ion-ios-navigate" ></i >
{{cafe.distance | number:"0"}}m</span >
</p >
<p >
<i class="ion ion-ios-location" ></i >
{{cafe.location.display_address | join:", " }}
</p >
<p >
<span >
<i class="ion ion-star" ></i >
{{cafe.rating}}</span >
<span >
<i class="ion ion-person" ></i >
{{cafe.review_count}}</span >
</p >
<ion-option-button class="button-positive"
ng-click="getDirections(cafe)">
<i class="ion ion-map"> Direction</i>
</ion-option-button>
</ion-item >
</ion-list >
<ion-infinite-scroll
immediate-check="false"
ng-if="yelp.hasMore"
on-infinite="loadMore()">
</ion-infinite-scroll>
</ion-content >
i have tried 5 days trying to figure it out , i have google every where please i need your help