Skip to content

Instantly share code, notes, and snippets.

@13122310958
Forked from 3dd13/controllers.js
Created February 17, 2017 03:07
Show Gist options
  • Save 13122310958/ace4b309ae1c8e6b080adcebd9686750 to your computer and use it in GitHub Desktop.
Save 13122310958/ace4b309ae1c8e6b080adcebd9686750 to your computer and use it in GitHub Desktop.
Testing ngCordova geolocation watchPosition and getCurrentPosition function
.controller('DashCtrl', function($scope, $ionicPlatform, $cordovaGeolocation) {
var watch;
var watchOptions = {
timeout : 5000,
maximumAge: 3000,
enableHighAccuracy: true // may cause errors if true
};
var pollCurrentLocation = function() {
$cordovaGeolocation.getCurrentPosition(watchOptions)
.then(function (position) {
var lat = position.coords.latitude
var long = position.coords.longitude
console.log('polling lat long', lat, long);
$scope.lastPollingLocation.lat = $scope.currentPollingLocation.lat;
$scope.lastPollingLocation.long = $scope.currentPollingLocation.long;
$scope.currentPollingLocation.lat = lat;
$scope.currentPollingLocation.long = long;
}, function(err) {
// error
console.log("polling error", err);
});
setTimeout(pollCurrentLocation, 1000);
};
var watchCurrentLocation = function() {
watch = $cordovaGeolocation.watchPosition(watchOptions);
watch.then(
null,
function(err) {
// error
console.log("watch error", err);
},
function(position) {
var lat = position.coords.latitude
var long = position.coords.longitude
console.log('lat long', lat, long);
$scope.lastLocation.lat = $scope.currentLocation.lat;
$scope.lastLocation.long = $scope.currentLocation.long;
$scope.currentLocation.lat = lat;
$scope.currentLocation.long = long;
});
};
$scope.lastLocation = {
lat: null,
long: null
};
$scope.currentLocation = {
lat: null,
long: null
};
$scope.lastPollingLocation = {
lat: null,
long: null
};
$scope.currentPollingLocation = {
lat: null,
long: null
};
$ionicPlatform.ready(function() {
watchCurrentLocation();
pollCurrentLocation();
});
$scope.$on("$destroy", function() {
if (watch) {
watch.clearWatch();
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment