定位1.微信小程序按钮切换获取定位权限一个授权,一个设置

tech2022-08-05  130

详见:码云小程序demo1 1.wxml: <button wx:if="{{isok}}" bindtap="getAddress">获取定位</button> <!-- 先显示提示框要求用户给予权限 --> <button wx:else bindtap="getAddress1">获取定位</button> <!-- 如果用户拒绝,则显示此框,点击弹到设置页面 --> 2.app.json "permission": { "scope.userLocation": { "desc": "你的位置信息将用于小程序位置接口的效果展示" } }, //要想获取定位功能首先要用户授权 3.js: import Router from '../../utils/index' Router({ /** * 页面的初始数据 */ data: { isok: true //开始为true,弹出授权框 }, getAddress() { wx.getLocation({ //用户授权后获取定位 success: ({ //成功解构出经纬度并打印 latitude, longitude }) => { console.log(latitude, longitude); }, fail: () => { //失败,弹出提示框:为了不影响您的使用,请授权定位 this.$msg('为了不影响您的使用,请授权定位') this.$set('isok', false) //失败,显示设置框 } }) }, getAddress1() { //点击设置框 wx.openSetting({ //弹出设置页面,显示已向用户请求过的授权 success: (res) => { //成功 if (res.authSetting["scope.userLocation"]) { //如果为true,显示授权框,并解构出用户经纬度,打印 this.$set('isok', true) wx.getLocation({ success: ({ latitude, longitude }) => { console.log(latitude, longitude); } }) } else { //如果还是未授权,提示用户 this.$msg('为了不影响您的使用,请授权定位') } } }) }, })
最新回复(0)