user4495161
user4495161

Reputation:

Ionic & ngCorgova Geolocation

I'm currently working on an Ionic / ngCordova Geolocation project were we want to be able to get the users current location.

I've managed to get the users location but unable to get the timeout refresh to continue seeing their location whilst moving.

Any advice/help would be highly appreciated.

   (function(angular, undefined) {
      "use strict";
      var app = angular.module('starter');

      app.controller('geoCtrl', function($cordovaGeolocation, $scope) {
        var self = this;

        var posOptions = {enableHighAccuracy: true, maximumAge: 5000, timeout: 8000};
        $cordovaGeolocation
          .getCurrentPosition(posOptions)
          .then(function (position) {
            var lat  = position.coords.latitude;
            var long = position.coords.longitude;
            self.location = lat +' - '+ long;
            console.log(self.location);
          }, function(err) {
            console.log('error:', error);
          });

        var watchOptions = {
          timeout : 1000,
          enableHighAccuracy: false
        };
        var watch = $cordovaGeolocation.watchPosition(watchOptions);

        watch.then(
          null,
          function(err) {
          },
          function(position) {
            var lat  = position.coords.latitude;
            var long = position.coords.longitude;
            self.location = lat +' - '+ long;
            console.log(self.location);
        });

        watch.clearWatch();

      });
    })(angular);

Upvotes: 0

Views: 453

Answers (1)

LeftyX
LeftyX

Reputation: 35587

You're calling watch.clearWatch() and what that does is, basically, reset the watcher.

According to the plugin's documentation

navigator.geolocation.clearWatch

Stop watching for changes to the device's location referenced by the watchID parameter.

navigator.geolocation.clearWatch(watchID);

Just comment that line and everything should work fine.

Upvotes: 1

Related Questions