微信小程序判断是否开启定位功能&引导重新打开定位权限

初心-杨瑞超个人博客诚邀您加入qq群(IT-程序猿-技术交流群): 757345416丨(IT-程序猿-技术交流2群): 936929828

需求描述

用户使用小程序时拒绝了定位权限,但是在必须需要定位时,想要重新打开时操作不易,所以要引导去设置里重新打开。

解决方案

当第一次关闭授权定位后,wx.chooseLocation就会一直调用fail方法,所以当点击打开位置功能时,先判断当前是否关闭了定位授权(见wx.getSetting文档详情),再会跳出自定义弹框(layerModel),引导前往设置打开定位即可

解析

// data中layerModel默认为false

wx.chooseLocation({
      success: function(e) {
       //允许打开定位
      },
      fail: () => {
      //不允许打开定位
        wx.getSetting({
          success: (res) => {
            if (!res.authSetting['scope.userLocation']) {
            //打开提示框,提示前往设置页面
              this.setData({
                layerModel: true
              })
            }
          }
        })
      }
    })

changeModalCancel () {
	console.log('取消回调');
}

由于前往设置需要使用button的open-type,因此弹框需要自定义,例如下方,不需要弹框的情况下引用button组件即可(详情见官方文档)

<view  wx:if="{{layerModel}}" class="modal-mask">
    <view class="modal-dialog">
      <view class="modal-title">温馨提示</view>
      <view class="modal-content">
        获取定位失败,请前往设置打开定位权限
      </view>
      <view class="modal-footer">
        <view class="btn-cancel" catchtap="changeModalCancel">取消</view>
        <button open-type="openSetting" class="btn-confirm" catchtap="changeModalCancel">设置</button>
      </view>
    </view>
  </view>
.modal-mask {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.5);
    overflow: hidden;
    z-index: 999999;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .modal-dialog {
    width: 540rpx;
    overflow: hidden;
    z-index: 999999;
    background: #f9f9f9;
    border-radius: 5rpx;
  }
  .modal-title {
    padding-top: 30rpx;
    font-size: 35rpx;
    color: #030303;
    text-align: center;
  }
  .modal-content {
    padding: 30rpx;
    font-size: 32rpx;  
  }
  .modal-footer {
    display: flex;
    flex-direction: row;
    height: 86rpx;
    border-top: 1px solid #dedede;
    font-size: 35rpx;
    line-height: 86rpx;
  }
  .btn-cancel {
    width: 50%;
    color: #abb4bd;
    text-align: center;
    border-right: 1px solid #dedede;
  }
  .btn-confirm {
    height: 86rpx;
    line-height: 86rpx;
    width: 50%;
    color: #6fb64b;
    text-align: center;
    font-size: 35rpx;
  }
  .btn-confirm::after {
    border: 0 !important;
}

文章到此结束,喜欢记得点个赞噢~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值