加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – cordova离子框架:获取位置后台服务

发布时间:2020-12-17 07:09:28 所属栏目:安全 来源:网络整理
导读:我正在尝试获取当前用户位置,但是当我重新加载代码时它会发生变化,即使我想每隔x秒获取一次该位置.我的解决方案: app.controller('geoCtrl',function($scope,$timeout,$cordovaBackgroundGeolocation,$ionicPlatform,$window,$cordovaGeolocation){var posO
我正在尝试获取当前用户位置,但是当我重新加载代码时它会发生变化,即使我想每隔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秒获取位置数据:)

解决方法

可能你想要的是这个:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读