鍍金池/ 問答/HTML/ 微信小程序wx.getLocation返回errMsg: "getLo

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

1. 微信小程序,想獲取當(dāng)前位置,結(jié)果調(diào)用wx.getLocation說沒有權(quán)限,我看官網(wǎng)的map組件也沒有授權(quán)部分的代碼啊。。。
2. 以下是我的代碼:
//獲取當(dāng)前位置的函數(shù)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. 然后報(bào)錯(cuò)信息就是這個(gè):
errMsg:"getLocation:fail no permission"
4. 根據(jù)回答改動(dòng)了一下,還是報(bào)錯(cuò)。。。我也很絕望,各位大神幫幫小白

試改動(dòng)代碼以及控制臺(tái)截圖

回答
編輯回答
淺時(shí)光

今天,貌似找到了問題出現(xiàn)的原因(? _ ?)
因?yàn)槲矣玫氖菧y試用的appid,然后
clipboard.png
驚覺我用的appid也是測試號(hào),不知道是不是這個(gè)原因了,但還是感謝這位同學(xué)~

2017年10月9日 14:23
編輯回答
爆扎

需要用戶授權(quán),需要用戶允許


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

//頁面加載時(shí)定位到用戶實(shí)際中心位置
  _getLocation: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    });
  },

你嘗試下這個(gè)代碼

圖片描述

參考:
獲取地址授權(quán)
微信官方

2017年5月30日 17:29