Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0d3baa3

Browse files
committed
feat: 小程序封装地图授权函数
1 parent f6c1f10 commit 0d3baa3

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- [如何使小程序请求优雅化](https://github.com/mtonhuang/blog/issues/14)【hot】
1616
- [小程序使用whistle代理](https://github.com/mtonhuang/blog/issues/15)
1717
- [小程序滑动分页加载动效](https://github.com/mtonhuang/blog/issues/33)
18+
- [小程序封装地图授权函数](https://github.com/mtonhuang/blog/issues/34)
1819
- 使用computed计算/watch观察
1920
- 小程序 + typescript
2021
- 小程序云上报

packages/promise-mini/map.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
### 一、封装地图授权函数
2+
3+
```js
4+
5+
// 1、点击事件后调起授权(app.ts,全局调用)
6+
async getAuthAdress(longitude: number, latitude: number, name: string, address: string) {
7+
let getLongitude = longitude,
8+
getLatitude = latitude,
9+
getName = name,
10+
getAddress = address;
11+
try {
12+
let resSetting = await wx.getSetting();
13+
console.log(resSetting)
14+
if (resSetting.errMsg === 'getSetting:ok' &&
15+
typeof resSetting.authSetting['scope.userLocation'] !== 'undefined' &&
16+
resSetting.authSetting['scope.userLocation'] === false) {
17+
//TODO: 没有授权
18+
console.log('拒绝了授权')
19+
wx.showModal({
20+
title: '提示',
21+
content: '需要获取您的地理位置,请确认授权,否则地图功能将无法正常使用',
22+
confirmText: '立即授权',
23+
success: async function (data) {
24+
if (data.confirm) {
25+
console.log('resModal.confirm')
26+
await wx.openSetting({
27+
28+
})
29+
} else if (data.cancel) {
30+
await wx.openLocation({
31+
latitude: Number(getLatitude),
32+
longitude: Number(getLongitude),
33+
scale: 14,
34+
name: getName,
35+
address: getAddress
36+
})
37+
}
38+
}
39+
})
40+
} else {
41+
//TODO: 授权
42+
await wx.getLocation({ type: 'gcj02' });
43+
await wx.openLocation({
44+
latitude: Number(getLatitude),
45+
longitude: Number(getLongitude),
46+
scale: 14,
47+
name: getName,
48+
address: getAddress
49+
})
50+
}
51+
}
52+
catch {
53+
//TODO: 报错
54+
}
55+
},
56+
57+
// 2、初始化页面调起地图授权函数(业务侧自我调用)
58+
async getLocation() {
59+
const that = this;
60+
try {
61+
let res = await wx.getSetting();
62+
if (res.authSetting['scope.userLocation'] !== true) {
63+
//TODO: 如果没有授权,则不需要操作
64+
that.getTest();
65+
} else {
66+
//TODO: 如果授权,则获取经纬度
67+
let res = await wx.getLocation({ type: 'gcj02' });
68+
let latitude = res.latitude;
69+
let longitude = res.longitude;
70+
if (latitude && longitude) {
71+
that.setData({
72+
user_latitude: `${latitude}`,
73+
user_longitude: `${longitude}`
74+
})
75+
}
76+
that.getTest();
77+
}
78+
}
79+
catch {
80+
that.getTest();
81+
//TODO: 报错
82+
}
83+
}
84+
85+
```
86+
87+
### 二、在业务页面调用
88+
89+
```js
90+
const app = getApp();
91+
// index.js
92+
let longitude = Number(e.currentTarget.dataset.longitude) || 0;
93+
let Latitude = Number(e.currentTarget.dataset.latitude) || 0;
94+
let Name = e.currentTarget.dataset.name || '';
95+
let Address = e.currentTarget.dataset.address || '';
96+
app.longitude(longitude, Latitude, Name, Address)
97+
98+
```
99+
100+
101+
**Contributing**
102+
103+
mtonhuang

0 commit comments

Comments
 (0)