微信小程序位置授权处理方法
作者:daisy 时间:2024-06-10 08:21:05
这两天在做小程序调取地图的时候遇到一个问题,如果用户第一次拒绝了位置权限请求。那么就不会再次唤起授权弹出。需要我们引导用户去开启。
具体做法如下。 在 aap.json中加入授权配置
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
}
在 page页面中使用,需要有几个注意的地方,初次使用的时候,去申请权限。这里我是放在onShow 方法里面。
onShow: function () {
//初始获取定位权限
wx.authorize({
scope: 'scope.userLocation',
success: (res) => {
},
})
},
然后在调用地图地位。或者获取用户权限的函数去判断是否有定位权限,如果没有那么引导用户开启权限。 如下:我有一个Input去触发选择地图事件。
<input bindfocus="openMap" value='{{address}}' placeholder="点击选择详细地址"></input>
事件处理
openMap:function(e){
var that = this
wx.getSetting({
success(res){
//这里判断是否有地位权限
if (!res.authSetting['scope.userLocation']) {
wx.showModal({
title: '提示',
content: '请求获取位置权限',
success:function(res){
if(res.confirm==false){
return false;
}
wx.openSetting({
success(res) {
//如果再次拒绝则返回页面并提示
if (!res.authSetting['scope.userLocation']) {
wx.showToast({
title: '此功能需获取位置信息,请重新设置',
duration: 3000,
icon: 'none'
})
} else {
//允许授权,调用地图
that.chooseMap()
}
}
})
}
})
} else {
//如果有定位权限,调用地图
that.chooseMap()
}
}
})
},
chooseMap(){
var that = this
wx.chooseLocation({
success: function (res) {
that.setData({
address: res.address,
latitude: res.latitude,
longitude: res.longitude
})
},
fail: function (res) {
console.log(res)
}
})
},
来源:https://www.daisc.net/show/Small-Program-Location-Authorization-Processing.html
标签:微信小程序,位置,授权
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Phar反序列化超详细介绍
2023-06-05 07:06:02
![](https://img.aspxhome.com/file/2023/8/55368_0s.png)
使用llama Index帮你训练pdf的示例详解
2023-07-30 23:00:33
![](https://img.aspxhome.com/file/2023/7/112387_0s.png)
实战手记:让百万级数据瞬间导入SQL Server
2010-06-07 14:13:00
![](https://img.aspxhome.com/file/UploadPic/20106/201067122858333s.jpg)
SQL Server 开窗函数 Over()代替游标的使用详解
2024-01-25 00:35:40
![](https://img.aspxhome.com/file/2023/5/86875_0s.png)
python如何对链表操作
2023-08-18 05:40:46
Vue 服务端渲染SSR示例详解
2024-05-28 15:50:39
![](https://img.aspxhome.com/file/2023/1/123161_0s.jpg)
ASP生成静态模版技术(带参数的标签)
2009-03-03 12:29:00
ASP生成柱型体,折线图,饼图源代码
2007-09-20 12:56:00
python+pyqt5实现KFC点餐收银系统
2022-02-05 08:49:46
![](https://img.aspxhome.com/file/2023/8/134718_0s.png)
玩转Python图像处理之二值图像腐蚀详解
2022-09-01 16:23:24
![](https://img.aspxhome.com/file/2023/1/82611_0s.png)
Python中处理字符串之endswith()方法的使用简介
2023-02-25 09:05:36
Vue中computed和watch的区别
2024-05-29 22:22:50
![](https://img.aspxhome.com/file/2023/4/123174_0s.webp)
Windows下对MySQL安装的故障诊断与排除
2008-12-17 16:50:00
在VScode中引用自定义模块问题
2023-08-13 03:50:37
![](https://img.aspxhome.com/file/2023/3/111943_0s.png)
Python实现根据IP地址和子网掩码算出网段的方法
2021-11-20 01:40:41
浅谈FastClick 填坑及源码解析
2024-04-10 16:08:57
![](https://img.aspxhome.com/file/2023/0/136790_0s.gif)
JS中的THIS和WINDOW.EVENT.SRCELEMENT详解
2023-07-20 20:48:51
sqlserver下Kill 所有连接到某一数据库的连接
2024-01-21 18:05:51
Oracle数据库中SQL语句的优化技巧
2024-01-27 01:33:27
关于jupyter lab安装及导入tensorflow找不到模块的问题
2023-05-13 18:03:32