【问题标题】:cordova ionic framework : get location background service科尔多瓦离子框架:获取位置后台服务
【发布时间】:2016-03-11 19:50:01
【问题描述】:

我正在尝试获取当前用户位置,但是当我重新加载代码时它会发生变化,即使我想每隔 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 秒获取一次位置数据 :)

【问题讨论】:

  • 看起来您在创建手表后立即清除了它...
  • 我想每 30 秒取一次结果!!!

标签: angularjs cordova service location ionic


【解决方案1】:

你想要的大概是这样的:

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 Geolocation docs

【讨论】:

  • thank youuuuuuu ,这就是解决方案 :) 非常非常非常非常感谢 ^^
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 2016-08-30
    相关资源
    最近更新 更多