0

我正在尝试cordovaGeolocation链接我已经在我的应用程序中插入了相同的代码当应用程序打开时它对我来说工作正常,我需要在应用程序处于后台时获取此地理位置并将其发送到服务器,所以使用后台模式cordova见backgroundMode文档链接

  1. 当应用程序打开时,我正在获取地理位置并发送到服务器工作正常。
  2. 我也将 onpause 和 onresume cordova 用于原生 android 中的应用程序状态。
  3. 当应用程序处于暂停状态时,我在 onactivate() 内部使用了后台 enable(),给定时间间隔并获取地理位置并发送到服务器。
  4. 一旦应用程序进入 onpause 并返回到 onresume 应用程序就可以完美地获取地理位置并发送到服务器。

 app.run(['$rootScope', '$location','$mdToast','$cordovaPush', '$sessionStorage', '$cordovaDevice',
 '$cordovaLocalNotification', '$timeout','$cordovaGeolocation','$interval','Service',
  function($rootScope, $location,$mdToast,$cordovaPush, $sessionStorage, $cordovaDevice,
   $cordovaLocalNotification, $timeout,$cordovaGeolocation,$interval,Service) {
document.addEventListener("resume", function() {
               console.log("The application is resuming from the background");
  cordova.plugins.backgroundMode.disable();
           }, false);
           document.addEventListener("pause", function() {
                    console.log("The application is pausing to the background");
                    cordova.plugins.backgroundMode.enable();
                }, false);     
      var i=0;
      cordova.plugins.backgroundMode.onactivate = function () {
        console.log("notificationReceived");
      setInterval(function () {
        console.log("interval: "+ i++);
        var posOptions = {timeout:3000,enableHighAccuracy: false};
        $cordovaGeolocation
          .getCurrentPosition(posOptions)
          .then(function (position) {
            var GeoLocation=new Object();
            GeoLocation.geo_location_latitude=position.coords.latitude;
            GeoLocation.geo_location_longitude=position.coords.longitude;
            Service.CurrentService(GeoLocation).success(function(data){
              console.log(new Date());
              console.log("cur loc: "+JSON.stringify(data));
            }).error(function(data){
              console.log("error: "+JSON.stringify(data));
            })
          }, function(err) {
            console.log("Error: "+err);
          // here Position Error Occurs
            // error
          });
},60000);
     }
      })];

错误是当应用程序关闭或进入暂停状态时,收到Position Error 消息:Timeout expired while getting geolocation and not sent data to server。请参阅此编码并指导我完成此操作。先感谢您。

4

1 回答 1

0

你可以试试这个:

app.run(['$rootScope', '$location','$mdToast','$cordovaPush', '$sessionStorage', '$cordovaDevice',
 '$cordovaLocalNotification', '$timeout','$cordovaGeolocation','$interval','Service',
  function($rootScope, $location,$mdToast,$cordovaPush, $sessionStorage, $cordovaDevice,
   $cordovaLocalNotification, $timeout,$cordovaGeolocation,$interval,Service) {
document.addEventListener("resume", function() {
               console.log("The application is resuming from the background");
  cordova.plugins.backgroundMode.disable();
           }, false);
           document.addEventListener("pause", function() {
                    console.log("The application is pausing to the background");
                    cordova.plugins.backgroundMode.enable();
                }, false);     
      var i=0;
      cordova.plugins.backgroundMode.onactivate = function () {
        console.log("notificationReceived");
      setInterval(function () {
        console.log("interval: "+ i++);
        var posOptions = {timeout:3000,enableHighAccuracy: true};
        $cordovaGeolocation
          .getCurrentPosition(posOptions)
          .then(function (position) {
            var geo = {};
            geo.location_latitude=position.coords.latitude;
            geo.location_longitude=position.coords.longitude;
            Service.CurrentService(geo).success(function(data){
              console.log(new Date());
              console.log("cur loc: "+JSON.stringify(data));
            }).error(function(data){
              console.log("error: "+JSON.stringify(data));
            })
          }, function(err) {
            console.log("Error: "+err);
          // here Position Error Occurs
            // error
          });
},60000);
     }
      })];

或者你可以改变:

    $http({
    url : 'yourserverURL',
      method: 'POST',
      data: obj,
     header:{'Content-Type':'application/json; charset=utf-8'}

   }).success(function (data,status,headers,config){
              console.log(new Date());
              console.log("cur loc: "+JSON.stringify(data));

   }).error(function (data,status,headers,config){});

反而

Service.CurrentService(geo).success(function(data){
                  console.log(new Date());
                  console.log("cur loc: "+JSON.stringify(data));
                }).error(function(data){
                  console.log("error: "+JSON.stringify(data));
                })

**您也可以尝试将其放置在控制器中

于 2016-03-12T07:54:23.070 回答