检测小程序是否授权定位 余获取定位

706 阅读1分钟

检测小程序是否获取定位

需求

** 1.用户手机硬件定位没开启 ** * 提示 *

** 2.小程序没有授权定位 ** * 提示 *

第一步 app.json 添加

  "permission": {
    "scope.userLocation": {
        "desc": "你的位置信息将用于小程序位置接口展示"
    }


第二步 我一般这个放app.js


  isGetlocation(cb) {
    var that = this
    wx.getSetting({
      success(res) {
        //这里判断是否有地位权限
        if (!res.authSetting['scope.userLocation']) {
          wx.showModal({
            title: '提示',
            content: '请求获取位置权限',
            success: function (res) {
              if (res.confirm == false) {
                return false;
              }
              wx.openSetting({
                success(res) {
                  //如果再次拒绝则返回页面并提示
                  if (!res.authSetting['scope.userLocation']) {
                    wx.showToast({
                      title: '此功能需获取位置信息,请重新设置',
                      duration: 3000,
                      icon: 'none'
                    })
                  } else {
                    //允许授权,调用地图
                    cb()  // 回调地址
                  }
                }
              })
            }
          })
        } else {
          //如果硬件没有授权提授权
          wx.showModal({
            title: '您手机定位功能没有开启',
            content: '请在系统设置中打开定位服务',
            success() {
                   // 跳到首页 按需操作
              
            }
          })
        }
      }
    })
  },
    
    
    
    


这个可以放进app.js 方便在其他页面直接 getApp().isGetlocation(()=>{ 回调成功 定位权限已获取 })