Skip to content

Instantly share code, notes, and snippets.

@3dd13
Created June 15, 2015 12:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save 3dd13/a3907b18c9520354e97b to your computer and use it in GitHub Desktop.
Save 3dd13/a3907b18c9520354e97b 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