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

前端之家收集整理的这篇文章主要介绍了angularjs – cordova离子框架:获取位置后台服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试获取当前用户位置,但是当我重新加载代码时它会发生变化,即使我想每隔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

猜你在找的Angularjs相关文章