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

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

要回复文章请先登录注册

来学习

来学习

大佬怎么调用呢 ?
2022-09-28 16:00
白罂粟

白罂粟 (作者)

回复 2***@qq.com :
要写定时器啊 而且这个是改变定位出发不是一直获取定位的方法
2022-08-24 13:12
2***@qq.com

2***@qq.com

回复 2***@qq.com :
我现在的做法也是将获得的信息塞进数组里,只是把上传给服务器的方法也放到了定时器当中,这样做在安卓里并不能防杀,息屏过几分钟就收不到消息了,请问您是用什么技巧实现的后台持续定位呢?
2022-08-15 10:15
2***@qq.com

2***@qq.com

大佬你好,我自己写了一个简单的用的uni.getLocation(OBJECT)封装好的方法,钻研您代码的时候,发现您并没有调用uniapp的方法,是原生的方法吗?
2022-08-15 10:10
白罂粟

白罂粟 (作者)

回复 追光者 :
放到哪里都可以的 必须是唯一的
2022-07-04 11:25
追光者

追光者

回复 白罂粟 :
大佬你好,你的持续定位的文章,上了官方的推荐
我有点不明白,这个源码放到项目的什么位置?怎么用呢?
2022-06-27 22:56
白罂粟

白罂粟 (作者)

回复 小关关啊 :
用于当前坐标改变的情况 如果一直不动只会上传当前位置的坐标
2022-06-24 14:12
小关关啊

小关关啊

大佬有没有具体使用方法
2022-06-20 10:31
3***@qq.com

3***@qq.com

大佬,求份源码,跪谢 3185852922@qq.com
2022-04-15 09:52
l王l

l王l

大佬,能求份源码吗,谢谢啦,925836803@qq.com
2022-04-14 09:17