WeChatミニプログラム-ユーザーが位置情報の要求を拒否した場合、位置認証を設定するようにユーザーに通知します

図に示すように、
ここに画像の説明を挿入
ユーザーが[キャンセル]をクリックすると、位置情報の要求は拒否されました。次の図に示すように、地理的位置認証を設定するようにユーザーに通知します
ここに画像の説明を挿入

関連するコードは次のとおりです。

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'
        })
    }
  },

参照元:https://blog.csdn.net/weixin_41625322/article/details/83312354

おすすめ

転載: blog.csdn.net/z3287852/article/details/111402386