wx.onLocationChange获取不到speed数据,问题出在哪里?
发布于 6 年前 作者 bdong 3026 次浏览 来自 官方Issues

遇到的问题:开发一个上报地理位置的小程序,打包成体验版在ios下测试;调用startLocationUpdateBackground接口后,在onLocationChange中可以正常获得数据,除了speed一直返回-1。有哪位大神知道问题在哪里吗?


var that=this;


wx.startLocationUpdateBackground({

  success: function () {
    console.info("持续定位初始化成功");
    wx.onLocationChange(function (res) {
      console.info('[' + util.formatTime(new Date()) + ']位置改变:', res);
      that.setData({
        currentLongitude: res.longitude,
        currentLatitude: res.latitude,
        speed: res.speed,
        altitude: res.altitude
      });
      that.doReport();          
    })
  },
  fail: function () {
    console.info("持续定位初始化失败");
    wx.getSetting({
      success(res) {
        if (res.authSetting["scope.userLocationBackground"] != true ){
          wx.showModal({
            title: '提示',
            content: '您需要允许(使用小程序期间和离开小程序后)选项',
            success: function (res) {
              if (res.confirm) { //这里是点击了确定以后
                wx.openSetting({
                  success(res) {
                    that.doMain();
                  },
                  fail: function () {
                    console.info("打开设置失败")
                  }
                });
              } else { //这里是点击了取消以后
                //console.log('用户点击取消')
              }
            }
          })
        } else { console.info("未知错误"); }
      }
    })       
  }
})
4 回复

谢谢,你的代码对我有用。

锁屏下:

  1. 小米MIX3 锁屏下的情况 startLocationUpdateBackground 返回的定位不准 (错乱),而且间隔很大。
  2. 黑鲨1 锁屏下的情况下 可以正常。
  3. iphone 11 锁屏下的情况下也可以正常

麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html

我刚试下了,也是返回-1

回到顶部