—— 阿尔伯特·爱因斯坦

程序员の奇妙冒险

小程序小程序
2025-04-09 10:55阅读:81评论:0

微信小程序-地理位置获取

获取定位2

1
2
<button bindtap="tomap" class="cu-btn line-blue sm"></button>
 
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
tomap: function() {
var t = this;
wx.authorize({
scope: "scope.userLocation",
success: function(i) {
wx.chooseLocation({
success: function(i) {
t.setData({
latitude: i.latitude,
longitude: i.longitude,
address: i.address
}), e = new a({
key: "HEQBZ-R6TWR-3YHWF-WJACM-ZH6LE-3SFB6"
});
var d = i.latitude, s = i.longitude;
t.getCity(d, s);
},
fail: function() {
wx.getSetting({
success: function(e) {
e.authSetting["scope.userLocation"] || wx.showModal({
content: "请允许获取地理位置后再次尝试",
success: function(e) {
e.confirm ? wx.openSetting({
success: function(e) {
e.authSetting = {
"scope.userInfo": !0,
"scope.userLocation": !0
};
},
fail: function(e) {
console.log(e);
}
}) : e.cancel && console.log("用户点击取消");
}
});
}
});
}
});
}
});
},
 
_PROTECTED0__
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
methods: {
isGetLocation() { // 3. 检查当前是否已经授权访问scope属性,参考下截图
var _this = this;
let a = "scope.userLocation";
uni.getSetting({
success(res) {
if (!res.authSetting[a]) { //3.1 每次进入程序判断当前是否获得授权,如果没有就去获得授权,如果获得授权,就直接获取当前地理位置
_this.getAuthorizeInfo()
} else {
_this.getLocation()
}
}
});
},
 
getAuthorizeInfo() { //1. uniapp弹窗弹出获取授权(地理,个人微信信息等授权信息)弹窗
var _this = this;
let a = "scope.userLocation";
uni.authorize({
scope: a,
success() { //1.1 允许授权
_this.getLocation();
},
fail() { //1.2 拒绝授权
console.log("你拒绝了授权,无法获得周边信息 ")
// 弹出授权确认框
_this.getSetFun();
}
})
},
// 没有获取到位置,不停获取
getSetFun() {
let _this = this;
wx.getSetting({
success(res) {
if (!res.authSetting['scope.userLocation']) {
wx.showModal({
title: '是否授权当前位置',
content: '请确认授权,否则无法正常使用',
success(res) {
if (res.confirm) {
wx.openSetting({
success() {
// 设置获取允许获取位置后重新调用获取定位方法或者跳到首页
_this.getLocation();
}
})
} else if (res.cancel) {
// 跳到首页
}
}
})
} else {
//用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
wx.showModal({
title: '您手机定位功能没有开启',
content: '请在系统设置中打开定位服务',
success() {
// 跳到首页
}
})
}
}
})
},
getLocation() {
var that = this;
 
var myAmapFun = new amapFile.AMapWX({
key: mapKey
});
myAmapFun.getPoiAround({
iconPath: 'https://s2.ax1x.com/2020/03/10/8CvKmt.png',
iconWidth: 22,
iconHeight: 32,
success: function(reset) {
that.initMap(reset.markers[0])
// console.error("高德地图", JSON.stringify(reset))
 
},
fail: function(info) {
uni.showToast({
icon: 'none’',
title: '位置获取失败,请允许小程序获取位置权限',
duration: 2000
})
}
})
},
// 初始化我的位置
async initMap(res) {
// this.longitude = res.longitude;
// this.latitude = res.latitude;
this.addrss = await this.getAddressName(res);
console.error("定位", this.addrss);
// this.addressObj = Object.assign({}, this.addressObj,{
// longitude: res.longitude,
// latitude: res.latitude,
// address: this.myAddress
// })
// console.error('初始化我的位置:' + JSON.stringify(this.addressObj));
 
},
// 查询地图中心点的名称
getAddressName(addressObj) {
let that = this;
let url = 'https://restapi.amap.com/v3/geocode/regeo?key=' + "7e38dcb5deed0f1f27360f8f4f63c3bf" +
"&location=" + addressObj.longitude + "," + addressObj.latitude + "&extensions=all";
console.log("url", url);
return new Promise((resrt) => {
uni.request({
url: url,
data: {
// key: "c1560fe15fca882115da0018d7cf42df",
// location: addressObj.longitude + "," + addressObj.latitude,
// extensions: "all"
//     s: "rsx",
//     sdkversion: "sdkversion",
//     logversion: "logversion"
},
success: function(e) {
// console.log("响应数据", JSON.stringify(e));
let addressComponent = e.data.regeocode.addressComponent;
let addrss = addressComponent.city +
addressComponent.district +
addressComponent.streetNumber.street +
addressComponent.streetNumber.number;
// resrt(e.data.regeocode.formatted_address); // 省市区街道几号
resrt(addrss);
// that.setData({
// textData: {
// name: res.data.regeocode.pois[0].name,
// desc: res.data.regeocode.formatted_address
// }
// })
// console.log(JSON.stringify(res.data.regeocode.formatted_address));
},
fail: function(rest) {
uni.showToast({
icon: 'none’',
title: '位置获取失败',
duration: 2000
})
resrt(rest);
}
});
})
},
}
 
评论(0)
暂无评论来抢沙发吧~