getpos.js
3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// components/userinfo/getpos.js
let failCount=0;
Component({
/**
* 组件的属性列表
*/
properties: {
hasAllowFail:{//强制获取定位
type:Boolean,
value:false,
},
hasAllowaAuthDeny:{//是否允许用户拒绝
type:Boolean,
value:true,
},
hasAllowLoading:{//包间查找中
type:Boolean,
value:true,
},
},
/**
* 组件的初始数据
*/
data: {},
attached(){
//this.getPos()
},
/**
* 组件的方法列表
*/
methods: {
getPos(fouce=false){
this._getPos().then(res => {
console.log('wx.getLocation.then', res);
failCount=0;
//this.triggerEvent("pos",res);
}).catch((e) => {
console.log('wx.getLocation.catch', failCount,e);
if(this.data.hasAllowFail){
this.triggerEvent("pos",{
latitude: '',
longitude: '',
});
if(this.data.hasAllowLoading) wx.hideLoading()
return;
}
if(failCount>2){//定位失败
failCount=0;
wx.showModal({
title:'定位失败',
content:'请开启位置服务',
showCancel:false,
success:()=>{
console.log('wx.showModal::success,给默认坐标');
this.triggerEvent("pos",{
//latitude: 39.6846701,
//longitude: 118.1763001,
latitude:'',
longitude:'',
});
if(this.data.hasAllowLoading) wx.hideLoading()
},
})
return;
}
if ('getLocation:fail auth deny' == e.errMsg
|| 'getLocation:fail:auth denied' == e.errMsg
|| 'getLocation:fail authorize no response' == e.errMsg) {
failCount=0;
if(this.data.hasAllowaAuthDeny && !fouce){
this.triggerEvent("pos",{
latitude: '',
longitude: '',
});
if(this.data.hasAllowLoading) wx.hideLoading()
return;
}
this.selectComponent('#showModal').options({
header: '定位失败',
body: '需要地理位置授权'
}).show();
} else {
failCount=failCount+1;
setTimeout(() => {
this.getPos()
}, 500)
}
})
},
_getPos(){
return new Promise((success, fail) => {
let rex={latitude:'',longitude:''};
if(this.data.hasAllowLoading){
wx.showLoading({
title: '包间查找中',
})
}
let hasSuccess=false;
wx.getLocation({
type: 'gcj02',
success: (res) => {
hasSuccess=true;
rex=res;
this.triggerEvent("pos",res);
success(res)
},
fail: (res) => {
fail(res)
},
complete: (res) => {
console.log('wx.getLocation', res)
}
});
/*
setTimeout(()=>{
fail({errMsg:'getLocation:fail auth deny'})
},3000)
*/
setTimeout(()=>{
if(!hasSuccess){
success(rex)
if(this.data.hasAllowLoading) wx.hideLoading()
}
},2000)
})
},
showModal(e){
wx.openSetting()
},
}
})