收藏
回答

微信公众号jssdk getLocation,为什么在安卓真机中没执行回调,而开发者工具中正常?

api: wx.getLocation

js-sdk是通过npm引入的 weixin-js-sdk v1.6.0 https://www.npmjs.com/package/weixin-js-sdk

wxJsApi (callback) {
  wx.checkJsApi({
    jsApiList: ['getLocation'],
    complete: (res) => {
      console.log(res)
    }
  })
  this.$axios({
    method: 'get',
    url: '/wechat/jsapi/signature', // config数据获取
    params: { url: window.location.href }
  }).then(res => {
    wx.config({
      beta: true,
      debug: true, // 开启调试模式,
      appId: res.data.data.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
      timestamp: res.data.data.timestamp, // 必填,生成签名的时间戳
      nonceStr: res.data.data.nonceStr, // 必填,生成签名的随机串
      signature: res.data.data.signature, // 必填,签名,见附录1
      jsApiList: ['getLocation'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
    })
  })
  wx.ready(() => {
    let isTimeOut = true
    const timeOut = setTimeout(() => {
      if (isTimeOut) {
        console.log('获取定位超时')
        Toast('获取定位超时')
      }
    }, 4000)
    wx.getLocation({
      type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
      success: (res) => {
        const latitude = res.latitude // 纬度,浮点数,范围为90 ~ -90
        const longitude = res.longitude // 经度,浮点数,范围为180 ~ -180。
        // const speed = res.speed // 速度,以米/每秒计
        // const accuracy = res.accuracy // 位置精度
        this.location = longitude.toFixed(6) + ',' + latitude.toFixed(6)
        console.log(this.location)
        Toast('获取定位:' + this.location)
        callback(this.location) // 回调(传入经纬度)
      },
      fail: (res) => {
        console.log('[error] wx.getLocation')
        console.log(res)
        Toast('获取定位失败\n' + JSON.stringify(res))
      },
      complete: (res) => {
        console.log('调用getLocation')
        console.log(res)
        Toast('调用getLocation\n' + JSON.stringify(res))
        isTimeOut = false
        clearTimeout(timeOut)
      }
    })
  })
  wx.error((response) => {
    console.log('[error] wx.ready')
    console.log(response)
    Toast('微信jsSdk初始化失败\n' + JSON.stringify(response))
    // this.formMess.fullAddress = JSON.stringify(res.data.data)
  })
},


1.在开发者工具中 Stable v1.03.2005140

功能正常,不会弹出获取定位超时

2.在安卓机中 v7.0.16

api debug提示调用getLocation成功,但没有执行任何回调函数(success,fail,complete)

3.更加神奇的是,在ios中,提示认证签名无效,主动刷新后功能正常了

最后一次编辑于  2020-07-17
回答关注问题邀请回答
收藏

1 个回答

  • 疯狂的小辣椒
    疯狂的小辣椒
    2020-07-17

    你好,麻烦具体描述问题流程,提供出现问题的具体机型、系统版本号,以及能复现问题的链接

    2020-07-17
    有用
    回复 1
    • Nguyen
      Nguyen
      2020-07-17
      关注“红葫芦云回收”公众号,点击“一键回收”菜单进入页面。页面流程:我的-->回收地址-->添加地址。在添加地址页面上可复现问题。


      正常情况是,获取好定位后,success回调,自动填写地区选择,并弹出消息“调用getLocation” 如图
      安卓手机小米8,oppo21均有问题。iphone8没有问题。
      2020-07-17
      回复
登录 后发表内容
问题标签