• 小程序判断是否授权位置信息和手动授权


    概要

    当用户来到页面之后就会弹出是否授权弹框,但是如果第一次关闭之后,下一次再过来的话页面的授权弹框就不出现了,针对于这种情况写了一个方法

    微信小程序的,使用的是高德地图

    	getLocationPodel() {
    				let that = this
    			
    					wx.getSetting({
    					    success: (res) => {
    					     if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
    						  wx.showModal({
    					       title: '是否授权当前位置',
    					       content: '需要获取您的地理位置,请确认授权,否则无法获取您所需数据',
    					       success: function (res) {
    					        if (res.cancel) {
    					         
    					        } else if (res.confirm) {
    					         wx.openSetting({
    					          success: function (dataAu) {
    					           if (dataAu.authSetting["scope.userLocation"] == true) {
    					            wx.showToast({
    					             title: '授权成功',
    					             icon: 'success',
    					             duration: 1000
    					            })
    					            //再次授权,调用getLocationt的API
    					            that.getLocation1();
    					           } else {
    					            wx.showToast({
    					             title: '授权失败',
    					             icon: 'success',
    					             duration: 1000
    					            })
    					           }
    					          }
    					         })
    					        }
    					       }
    					      })
    					     } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入
    					      that.getLocation1();
    					     }
    					     else { //授权后默认加载
    					      that.getLocation1();
    					     }
    					    }
    					})
    			
    			},
    			getLocation1() {
    				let that = this
    				uni.getLocation({
    					type: 'wgs84',
    					success: function (res) {
    						that.getLocation(res.latitude,res.longitude)
    					},
    					fail:function(res) {
    						console.log('dfvdfvdfv')
    					}
    				});
    			},
    			getLocation(latitude,longitude) {
    				let that = this
    				 //根据经纬度获取城市信息
    				wx.request({
    					url: 'https://apis.map.qq.com/ws/geocoder/v1',
    					data: {
    						key:'自己的key值,
    					    location:`${latitude},${longitude}`
    					},
    					success: (res) => {
    						let address = res.data.result.address_component
    						that.city = address.city
    						that.province = address.province
    					},
    					fail: () => {},
    					complete: () => {}
    				});
    			},
    
    • 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
  • 相关阅读:
    9.2 安卓逆向之—Frida持久化方案
    立体匹配入门指南(8):视差图、深度图、点云
    HCIP-MGRE实验
    PMP_第12章章节试题
    关于FPGA对 DDR4 (MT40A256M16)的读写控制 I
    PostgreSQL数据库统计信息——acquire_inherited_sample_rows采样函数
    从零开始学WEB前端——VUE脚手架
    Springboot分片下载实现
    kafka-listener
    通用工作站设计方案 :807-ORI-S3R500 -多路PCIe3.0的单CPU通用工作站
  • 原文地址:https://blog.csdn.net/weixin_52406641/article/details/134463577