白罂粟
白罂粟
  • 发布:2017-06-08 10:22
  • 更新:2022-09-28 16:00
  • 阅读:20829

ios持续定位/后台定位问题

分类:Native.js

我分享一下我解决IOS和Android定位后台执行定时上传方法。

文章最下面有zip源码下载文件

文章最下面有zip源码下载文件

文章最下面有zip源码下载文件

图1是声明定位的对象 图2是开启 location.js文件中全部封装好了你自己需要的方法 对外只需要暴露的checkLocation closeLocation 2个方法

定位对象必须要是唯一的 如果不唯一就会开启多个定位方法 影响使用

android的如果全杀死是没办法,在后台的情况是没问题的.
我的做法是用watchPosition将获取的到的坐标放在一个数组中,然后调用locationHandle方法每隔多少时间上次到服务器。
这个不会出现上传一段时间之后关掉的情况。
1.在这之前你需要在manifest.json文件中配置启用后台模块"UIBackgroundModes": ["location"]
2.如果定位失败会返回一个5e-324可能是你手机的定位权限没有开启。

var Location = function () {  
  this.reportLocation = null;  
  this.watchLocation = null;  
  this.locationPool = [];  
}  

/**  
 * 判断是否开启定位  
 * @param {Object} isTask 1是开启循环上传定位0是10分钟上传一次本地定位  
 */  
Location.prototype.checkLocation = function (isTask) {  
var _this = this;  
  _this.openLocation({  
                onSuccess: function () {  
                  //开启定位成功首页回调方法  
                  var sell_main = plus.webview.getWebviewById('sell_main');  
                  mui.fire(sell_main, 'locationSuccess');  
                },  
                onFailed: function () {  
                 //开启定位失败首页回调方法  
                  var sell_main = plus.webview.getWebviewById('sell_main');  
                  mui.fire(sell_main, 'locationFailure');  
                }  
              });  
}  

/**  
 * 开启定位  
 * @param {Object} options  
 */  
Location.prototype.openLocation = function (options) {  
  options = options || {};  
  var _this = this;  
  var isTask = this.isTask;  
  var driverForOrderObject = null;  
  var driverForTaskObject = null;  
  var userInfo = DbUtils.getStorage('userInfo', 1);  

  var locationParams = {  
    enableHighAccuracy: true,  
    geocode: false,  
    coordsType: "bd09ll",  
    provider: 'baidu'  
  };  

}  

/**  
 * 默认第一次开启定位的是否先获取 之后用来过滤偏差很大的点  
 * @param {Object} options  
 */  
Location.prototype.locationReady = function (options) {  
  options = options || {};  
  var isTask = this.isTask;  
  var _this = this;  

  var locationParams = {  
    enableHighAccuracy: true,  
    geocode: false,  
    maximumAge: ApiConfig.UPLOADLOCATIONWORKTIME,  
    coordsType: "bd09ll",  
    provider: 'baidu'  
  };  
  _this.getCurrentPosition()  
}  

/**  
 * 非工作状态定位,十五分钟上传一次定位  
 * @param  options  
 */  
Location.prototype.accurateLocation = function (options) {  
  options = options || {};  
  var _this = this;  
  plus.geolocation.getCurrentPosition(function (position) {  
    _this.locationPool.push(position);  
    ApiConfig.staticIsDebug('getCurrentPosition', JSON.stringify(position));  
    _this.reportLocationHandle({  
      onSuccess: function (success) {  
        options.onSuccess && options.onSuccess(success);  
      },  
      onFailed: function (failure) {  
        options.onFailed && options.onFailed(failure);  
      }  
    });  
  }, function () {  
    options.onFailed && options.onFailed();  
  }, options.locationParams);  
}  

/**  
 * 只要定位改变就会获取并过滤错误的点和偏差很多的点   
 *坐标为 -5e32是定位权限没开启的情况  
 * @param  options  
 */  
Location.prototype.getCurrentPosition = function (options) {  
  options = options || {};  
  var _this = this;  
  var phpTimeInt = utilsJs.phpTimeInt();  
  var isWork = options.isWork;  
  var locationParams = options.locationParams;  
  //初始化Location参数  
  if (_this.reportLocation) {  
    window.clearInterval(_this.reportLocation);  
    _this.reportLocation = null;  
  }  
  if (_this.watchLocation) {  
    plus.geolocation.clearWatch(_this.watchLocation);  
    _this.reportLocation = null;  
  }  

  //是否是工作状态 工作状态用watchPosition 非工作状态用getCurrentPosition  
  if (isWork) {  
    _this.watchLocation = plus.geolocation.watchPosition(function (position) {  
        //console.log(JSON.stringify(position));  
        var difference = (position.timestamp / 1000) - phpTimeInt;  
        var coords = position.coords;  
        if (coords.latitude > 0.00000001 && coords.longitude > 0.00000001 && difference > 1) {  
          phpTimeInt = (position.timestamp / 1000);  
          _this.locationPool.push(position);  
          _this.locationHandle({  
            onSuccess: function (success) {  
              options.onSuccess && options.onSuccess(success);  
            },  
            onFailed: function (failure) {  
              options.onFailed && options.onFailed(failure);  
            }  
          });  
        }  
      },  
      function () {  
        options.onFailed && options.onFailed();  
      }, locationParams);  
  } else {  
    _this.reportLocation = window.setInterval(function () {  
      _this.accurateLocation(options);  
    }, ApiConfig.UPLOADLOCATIONTIME);  
  }  
}  
//开启定时器  
Location.prototype.locationHandle = function (options) {  
  options = options || {};  
  var _this = this;  
  if (!_this.reportLocation) {  
    _this.reportLocation = window.setInterval(function () {  
      //plus.push.createMessage('locationPool:' + _this.locationPool.length, '', '');  
      _this.reportLocationHandle({  
        onSuccess: function (success) {  
          options.onSuccess && options.onSuccess(success);  
        },  
        onFailed: function (failure) {  
          options.onFailed && options.onFailed(failure);  
        }  
      });  
    }, ApiConfig.UPLOADLOCATIONWORKTIME);  
  }  
}  

/**  
 * 关闭定位  
 *  
 */  
Location.prototype.closeLocation = function () {  
  var _this = this;  
  var isTask = this.isTask;  
  if (_this.reportLocation) {  
    window.clearInterval(_this.reportLocation);  
    _this.reportLocation = null;  

  }  
  if (_this.watchLocation) {  
    plus.geolocation.clearWatch(_this.watchLocation);  
    _this.watchLocation = null;  
  }  
  this.locationPool = [];  
  if (isTask) {  
    DbUtils.removeItem('driverStatusForTask');  
  } else {  
    DbUtils.removeItem('driverStatusObject');  
  }  
}  

/**  
 * 上传定位接口  
 * @param options  
 */  
Location.prototype.reportLocationHandle = function (options) {  

}
16 关注 分享
lhyh 小白啊 DCloud_IOS_XTY 老哥教教我 8***@qq.com hello_XFJ asdasdsad Zenorz 2***@qq.com makeit 元梦 l***@163.com 2***@qq.com 追光者 2***@qq.com 1***@qq.com

要回复文章请先登录注册

gaus

gaus

回复 gaus :
大概看明白了
2019-04-29 07:07
gaus

gaus

回复 白罂粟 :
请问,工作和非工作状态是指什么? 多谢
2019-04-29 05:17
白罂粟

白罂粟 (作者)

回复 求知欲 :
地址留一个
2018-12-07 00:45
白罂粟

白罂粟 (作者)

回复 求知欲 :
完整的代码有很多我的逻辑反而影响阅读
2018-12-07 00:44
求知欲

求知欲

大佬能给个完整的代码吗
2018-12-04 18:02
苛学加

苛学加

mark
2018-11-05 18:09
白罂粟

白罂粟 (作者)

回复 1***@qq.com :
UIBackgroundModes只有ios需要 android可以的不过你不能把应用杀死
2018-09-26 16:07
1***@qq.com

1***@qq.com

回复 白罂粟 :
你好,谢谢回复。试了好多办法都没办法实现后台的持续定位,能给更详细一些的代码吗?android端没有UIBackgroundModes这个配置选项也可以吗?是否是工作状态,前台为工作状态,后台就为非工作状态的意思?
不管怎样,谢谢大佬提供隔段时间上传服务器的这个思路。
2018-09-26 15:44
白罂粟

白罂粟 (作者)

回复 1***@qq.com :
这个是android和ios都可以用的
2018-09-26 14:24
1***@qq.com

1***@qq.com

你好,请问一下android有办法做到类似的实现吗?
2018-09-18 17:30