WeChat Mini Program-When the user rejects the request for location information, remind the user to set the location authorization

As shown in the figure,
Insert picture description here
when the user clicked Cancel, the request for location information was rejected. As shown in the figure below, remind the user to set geographic authorization. As shown in the figure below,
Insert picture description here
if the user does not set it, this pop-up window will appear every time the user clicks to enter the page.

The relevant code is as follows:

wxml:

  <!-- 弹出层 -->
    <!--页面遮罩层 -->
    <view class="modal-mask" bindtap="hideModal"  wx:if="{
    
    {showFlag}}"></view>
    <!--页面提示弹窗 -->
    <view class="modal-dialog" wx:if="{
    
    {showFlag}}">
      <view class="t-title">您拒绝了地理位置授权,需要重新设置</view>
      <button class="showF" open-type="openSetting" bindopensetting="handler">去设置</button>   
    </view>

wxss:

/* 弹出层 */

/* 遮罩层*/
.modal-mask {
    
    
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: #000;
  opacity: 0.5;
  z-index:670;
}
 /* 页面提示弹窗*/
.modal-dialog {
    
    
  overflow: hidden;
  position: fixed;
  top: 20%;
  background: #f9f9f9;
  border-radius: 30rpx; 
  right: 5%;
  width: 90%;
  z-index: 680;
  color:#000;
}
.view-image{
    
    
  width: 580rpx;
  height: 580rpx;
  margin-left: 50rpx;
}
.guanbi{
    
    
  position:absolute;
  width: 40rpx;
  height:40rpx;
  background-size:100%;
  text-align: center; 
  top: 10px; 
  right: 10px;
  font-size: 14px;
  line-height: 50rpx;
}
.t-title{
    
    
  font-size:30rpx;
  text-align: center;
  margin: 30rpx 0;
  font-weight: bold;
}
.b-title{
    
    
  font-size:26rpx;
  text-align:center;
  margin-top:20rpx;
}

js:

data{
    
    
    showFlag:false
},

onLoad: function (options) {
    
    
let _this=this
    wx.getLocation({
    
    
        // type: 'wgs84',
       //返回可以用于wx.openLocation的经纬度
       //获取地理位置成功时
        success: function (res) {
    
    
        // 此处经纬度加减是根据实际情况处理的,处理之后在手机上比较准确
          let latitude = res.latitude + 0.001276
          let longitude = res.longitude + 0.006256
          _this.setData({
    
    
            latitude: latitude,
            longitude: longitude
          })
        },
       //获取地理位置失败(用户点击不允许)时执行
        fail: function () {
    
    
          wx.hideToast();
          _this.setData({
    
    
            showFlag: true
          })
        }
      })
},

//用户不允许时的提示,点击时去设置
  handler: function (e) {
    
    
    if (e.detail.authSetting["scope.userLocation"]) {
    
    
      this.setData({
    
    
        showFlag: false
      })
  //返回时重新刷新当前页面
      wx.reLaunch({
    
    
              url: '../index/index'
        })
    }
  },

Reference from: https://blog.csdn.net/weixin_41625322/article/details/83312354

Guess you like

Origin blog.csdn.net/z3287852/article/details/111402386