Skip to content

Instantly share code, notes, and snippets.

@3dd13
Created June 15, 2015 12:33

Revisions

  1. 3dd13 created this gist Jun 15, 2015.
    82 changes: 82 additions & 0 deletions controllers.js
    Original 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();
    }
    });

    })