微信小程序wx.getLocation返回errMsg: "getLocation:fail no permission"

1. 微信小程序,想获取当前位置,结果调用wx.getLocation说没有权限,我看官网的map组件也没有授权部分的代码啊。。。
2. 以下是我的代码:
//获取当前位置的函数getLaLo
getLaLo: function () {
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        console.log('this:',this)
        this.setData({
          la: latitude,
          lo: longitude
        }) 
      },
      fail: function(err) {
        console.log(err)
      },
    })
3. 然后报错信息就是这个:
errMsg:"getLocation:fail no permission"
4. 根据回答改动了一下,还是报错。。。我也很绝望,各位大神帮帮小白

试改动代码以及控制台截图

阅读 17.9k
4 个回答

需要用户授权,需要用户允许


onLoad: function (options) {
    this._getLocation();
},

//页面加载时定位到用户实际中心位置
  _getLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    });
  },

你尝试下这个代码

图片描述

参考:
获取地址授权
微信官方

今天,貌似找到了问题出现的原因(ಥ _ ಥ)
因为我用的是测试用的appid,然后
clipboard.png
惊觉我用的appid也是测试号,不知道是不是这个原因了,但还是感谢这位同学~

新手上路,请多包涵

app.json里加上这个试试 id要用自己的id 别用测试的

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