美文网首页
小程序,用户手机没有开启定位或没有授权定位的解决方案

小程序,用户手机没有开启定位或没有授权定位的解决方案

作者: world_7735 | 来源:发表于2020-06-11 13:38 被阅读0次

    我们先获取定位,如果用户拒绝或没有开手机定位都会进入fail里的getSetFun函数。通过跳转到首页已达到用户不开启定位或授权不能进行下一步。代码如下:

    onLoad(){
      let that = this;
      wx.getLocation({
        type: 'gcj02',
        success(res) {
              
        },
        fail(error) {
          that.getSetFun();  // 没有获取到位置,不停获取
        }
      })
    }
    // 没有获取到位置,不停获取
    getSetFun() {
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.userLocation']) {
            wx.showModal({
              title: '是否授权当前位置',
              content: '请确认授权,否则无法正常使用',
              success(res) {
                if (res.confirm) {
                  wx.openSetting({
                    success() {
                      // 跳到首页
                    }
                  })
                } else if (res.cancel) {
                      // 跳到首页
                }
              }
            })
          } else {
             //用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
             wx.showModal({
               title: '您手机定位功能没有开启',
               content: '请在系统设置中打开定位服务',
               success() {
                      // 跳到首页
               }
             })
           }
         }
       })
     }
    

    相关文章

      网友评论

          本文标题:小程序,用户手机没有开启定位或没有授权定位的解决方案

          本文链接:https://www.haomeiwen.com/subject/rklbtktx.html