-
Notifications
You must be signed in to change notification settings - Fork 90
/
app.js
68 lines (67 loc) · 2.19 KB
/
app.js
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
//app.js
const QQMapWX = require('./assets/libs/qqmap-wx-jssdk.min.js');
let qqmapsdk;
qqmapsdk = new QQMapWX({
key: 'MH2BZ-4WTK3-2D63K-YZRHP-HM537-HHBD3'
});
App({
onLaunch: function () {
this.initPage()
},
initPage(){
// 获取用户授权信息信息,防止重复出现授权弹框
wx.getSetting({
success: res => {
//已有权限直接获得信息,否则出现授权弹框
if (res.authSetting['scope.userLocation']) {
this.getUserLocation()
} else {
this.getUserLocation()
}
}
})
},
//获取用户的位置信息
getUserLocation() {
wx.getLocation({
//成功授权
success: (res) => {
const latitude = res.latitude;
const longitude = res.longitude;
// 使用腾讯地图接口将位置坐标转出成名称(为什么弹框出出现两次?)
qqmapsdk.reverseGeocoder({
location: { //文档说location默认为当前位置可以省略,但是还是要手动加上,否则弹框会出现两次,手机端则出现问题
latitude,
longitude
},
success: (res) => {
const cityFullname = res.result.address_component.city;
const cityInfo = {
latitude,
longitude,
cityName: cityFullname.substring(0, cityFullname.length - 1),
status:1
}
this.globalData.userLocation = { ...cityInfo} //浅拷贝对象
this.globalData.selectCity = { ...cityInfo } //浅拷贝对象
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回,所以此处加入 callback 以防止这种情况
if (this.userLocationReadyCallback) {
this.userLocationReadyCallback()
}
}
})
},
fail:()=>{
this.globalData.userLocation = {status:0}
//防止当弹框出现后,用户长时间不选择,
if (this.userLocationReadyCallback) {
this.userLocationReadyCallback()
}
}
})
},
globalData: {
userLocation: null, //用户的位置信息
selectCity: null //用户切换的城市
}
})