uniapp中地址授权

uniapp中地址授权

/**判断是否授权 */
			getSettingFun: function() {
			    let that = this;
			    //先通过 uni.getSetting 先查询一下用户是否授权了 "scope.userLocation" 这个 scope,也可直接调用uni.authorize请求授权;
			    uni.getSetting({
			      success: function(res) {
			        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
			        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
			        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
			        let status = res.authSetting["scope.userLocation"];
			        if (status != undefined && status != true) {
			          uni.showModal({
			            title: '请求授权当前位置',
			            content: '需要获取你的地理位置,请确认授权',
			            success: function(res) {
			              if (res.cancel) {
			                uni.showToast({
			                  title: '拒绝授权',
			                  icon: 'none',
			                  duration: 1000
			                })
			              } else if (res.confirm) {
			                uni.openSetting({
			                  success: function(res) {
			                    console.log("[uni.openSetting]", res)
			                    if (res.authSetting['scope.userLocation'] == true) {
			                      uni.showToast({
			                        title: '授权成功',
			                        icon: 'success',
			                        duration: 1000
			                      })
			                      //成功授权直接调用getlocation的API
			                      that.getLocationFunc();
			                    } else {
			                      uni.showToast({
			                        title: '授权失败',
			                        icon: 'none',
			                        duration: 1000
			                      })
			                    }
			                  }
			                })
			              }
			            }
			          })
			
			        } else if (status == undefined) {
			          //首次授权(undefined)
			          // 发起请求用户授权
			          uni.authorize({
			            scope: 'scope.userLocation',
			            success: function(res) {
			              //成功授权直接调用getlocation的API
			              that.getLocationFunc();
			            }
			          })
			        } else {
			          //成功授权直接调用getlocation的API
			          that.getLocationFunc();
			        }
			      }
			    })
			},
			//获取位置经纬度
			getLocationFunc(){
				return new Promise((resolve,reject)=>{
					uni.getLocation({
						type:"gcj02",
						success(res){
							resolve(res)
						}
					})
				})
			}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值