离子和ngCorgova Geolocation

时间:2015-11-19 14:09:58

标签: geolocation ionic-framework ionic ngcordova

我目前正致力于Ionic / ngCordova Geolocation项目,我们希望能够让用户获得当前位置。

我已设法获取用户位置但无法获得超时刷新以继续在移动时查看其位置。

任何建议/帮助都将受到高度赞赏。

   (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);

1 个答案:

答案 0 :(得分:1)

你正在呼叫saveOrUpdate(obj);,它的作用基本上是重置观察者。

根据插件的documentation

  

<强> navigator.geolocation.clearWatch

     

停止观察对设备引用的位置的更改   watchID参数。

     

watch.clearWatch()

只需评论该行,一切都应该正常。