1、获取自动到达开关
2、人员位置持续实时上传(获取人员当前位置)
3、获取要到达的指定位置的坐标点(可通过sdk地址解析)
4、比较两个坐标点之间的距离(sdk的距离计算)
5、当距离小于指定范围后,可调用到达接口或进行到达后的相关操作
Tips:wx.startLocationUpdateBackground()方法需注意以下:
- 安卓微信7.0.6版本,iOS 7.0.5版本起支持该接口
- 小程序的基础库 2.8.0 开始支持,低版本需做兼容处理。
- 需在app.json中配置requiredBackgroundModes: ['location']后使用
场景实例:
设置页面中有个自动达到的开关按钮,开启后需要实现自动到达功能。新工单中可有多条单子,一旦点击接单后,即可进行判断,当人员到达指定位置后,该条新工单的状态即变为已到达
1)获取自动到达开关(可在进入页面和watch监听中调用一下方法)
retrievalSwictch(){
//获取自动到达开关
wx.getStorage({
key:'switch',
success: res =>{
if(res.data==true){
//若到达中有单子,则获取人员位置,否则关闭位置上传
let that = this;
if (that.waitArriveListLength > 0) {
wx.getSetting({
success: (res) => {
if (!res.authSetting['scope.userLocation']) {
that.openConfirm()
} else {
if (res.authSetting['scope.userLocationBackground'] == true) {
that.getSelfLocation();
} else {
//如果关闭,则清缓存,关闭定时上传
this.clearLocaUpdate();
}
}
}
})
} else {
this.clearLocaUpdate();
}
}else{
this.clearLocaUpdate();
}
}
});
}
2)人员位置持续实时上传(获取人员当前位置)
tips:先确认用户是否打开定位权限
openConfirm() {
let that = this;
wx.showModal({
content: '自动到达功的实现需要您打开定位权限,是否去设置打开?',
confirmText: "确认",
cancelText: "取消",
success: function(res) {
//点击“确认”时打开设置页面
if (res.confirm) {
versionCompare.compareV('1.1.0', function() {
wx.openSetting({
success: (res) => {
console.log(res.authSetting);
if (res.authSetting['scope.userLocationBackground'] == true) {
that.getSelfLocation();
}
}
})
})
} else {
console.log('用户点击取消')
}
}
});
}
//获取人员当前人员位置
getSelfLocation() {
let that = this;
versionCompare.compareV('2.8.0', function() {
wx.startLocationUpdateBackground({
//开始
success: (res) => {
wx.onLocationChange((data) => {
//获取当前时间
var currentTime = new Date().getTime();
//获取上次保存的位置信息
var oldLocation = wx.getStorageSync('oldLocation');
//获取上次执行的时间
var oldTime = wx.getStorageSync('oldTime');
//将人员位置的经纬度拼接
var newLocation = data.latitude + "" + data.longitude;
//判断一下当前的位置是否和上次位置不一致
if (oldLocation != newLocation) {
//缓存当前最新位置
wx.setStorageSync('oldLocation', newLocation);
//缓存当前执行的时间
wx.setStorageSync('oldTime', currentTime);
//如果本次执行时间距离上次时间超过5s,将位置信息上传后台
if (currentTime - oldTime >= 5000) {
that.userLocate(); //调用接口,上传位置信息
that.currlongitude = data.longitude;
that.Currlatitude = data.latitude;
that.autoArrive(); //调用距离计算方法
}
}
});
},
fail: (err) => {
console.log("位置获取失败:", err)
}
})
})
}
3、获取要到达的指定位置的坐标点(可通过sdk地址解析)
参考:https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/methodGeocoder
4、比较两个坐标点之间的距离(sdk的距离计算)
// 自动到达
autoArrive() {
var _this = this;
for (var i = 0; i < _this.waitArriveList.length; i++) {
let item = _this.waitArriveList[i]
if (item.procCode == "02") {
//调用距离计算接口
qqmapsdk.calculateDistance({
//mode: 'driving',//可选值:'driving'(驾车)、'walking'(步行),不填默认:'walking',可不填
//from参数不填默认当前地址
//获取表单提交的经纬度并设置from和to参数(示例为string格式)
// from: e.detail.value.start || '', //若起点有数据则采用起点坐标,若为空默认当前地址
// to: e.detail.value.dest, //终点坐标
form: _this.Currlatitude.toString() + "," + _this.currlongitude.toString(),
to: item.latitude.toString() + "," + item.longitude.toString(),
success: function(res) { //成功后的回调
console.log("回调", res)
var res = res.result;
var dis = [];
for (var i = 0; i < res.elements.length; i++) {
dis.push(res.elements[i].distance); //将返回数据存入dis数组,
}
_this.distance = dis //设置并更新distance数据
if (_this.distance <= 500) {
console.log(_this.distance[0], "到达工单位置")
//到达对应位置后,调用到达接口
_this.handarrived(item)
} else {
console.log(_this.distance[0], "尚未到达")
}
console.log("成功", _this.distance);
},
fail: function(error) {
console.error(error);
},
complete: function(res) {
console.log(res);
}
});
}
}
}