Cordova Ionic框架:获取位置后台服务

3

我正在尝试获取当前用户位置,但每次重新加载代码时位置都会发生变化,即使我想每隔x秒获取一次位置。这是我的解决方案:

app.controller('geoCtrl', function($scope, $timeout, $cordovaBackgroundGeolocation, $ionicPlatform, $window, $cordovaGeolocation)
{
var posOptions = {timeout: 10000, enableHighAccuracy: false};
  $cordovaGeolocation
    .getCurrentPosition(posOptions)
    .then(function (position) {
      var lat  = position.coords.latitude
      var long = position.coords.longitude
      alert('lat='+lat);
      alert('long='+long);
    }, function(err) {
      // error
    });


  var watchOptions = {
    timeout : 3000,
    enableHighAccuracy: false // may cause errors if true
  };

  var watch = $cordovaGeolocation.watchPosition(watchOptions);
  watch.then(
    null,
    function(err) {
      // error
    },
    function(position) {
      var lat  = position.coords.latitude
      var long = position.coords.longitude
  });


  watch.clearWatch();

//-- End Geolocal
})

但这仅适用于我启动应用程序时,有人有想法吗?帮助我同步我的应用程序以每x秒获取位置数据 :)

看起来你是在创建后立即清除了这个表。 - Andre Kreienbring
我想每30秒获取一次结果!!! - Oumaya Abdelkhalek
2个回答

7
也许你需要的是这个:
app.controller('GeoCtrl', function($cordovaGeolocation, $interval) {

  var posOptions = {
    timeout: 10000,
    enableHighAccuracy: false
  };

  var getLocation = function() {
    $cordovaGeolocation
      .getCurrentPosition(posOptions)
      .then(function(position) {
        // location coordinates
        console.log(position.coords);
      })
      .catch(function(err) {
        // handle error
        console.log(err)
      });
  };

  $interval(getLocation, 3000); // 3 seconds for now

});

参考资料: ngCordova地理位置文档


非常感谢,这就是解决方案 :) 非常非常感谢 ^^ - Oumaya Abdelkhalek

0

你也可以使用 $cordovaGeolocation.watchPosition。你不需要手动应用 $interval。 但是这两种解决方案在 backgroundApp 的情况下都无法工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接