Created
June 15, 2015 12:33
Revisions
-
3dd13 created this gist
Jun 15, 2015 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,82 @@ .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(); } }); })