1. 程式人生 > 其它 >微信小程式-當用戶拒絕了位置資訊的請求時,提醒使用者去設定地理位置授權

微信小程式-當用戶拒絕了位置資訊的請求時,提醒使用者去設定地理位置授權

技術標籤:微信小程式

如圖
在這裡插入圖片描述
當用戶點選了取消,拒絕了位置資訊的請求。如下圖所示,提醒使用者去設定地理位置授權,如下圖所示,
在這裡插入圖片描述
如果使用者沒去設定的話,每次只要使用者點選進入該頁面都會有這個彈窗出現。

相關程式碼如下所示:

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