我想在小程序地图中监听实时地理位置变化,并将显示在地图上,不能用setData?

发布于 8 年前作者 linchao6290 次浏览最后编辑 8 年前来自 issues

需求:我想在小程序地图中监听实时地理位置变化,并将路线用线段显示在地图上,我使用的是wx.onLocationChange()

实现方式: 我将下面的函数定义在page外面,因为定义在page里面无法调用,会报错ReferenceError: _locationChangeFn is not defined

const _locationChangeFn = function(res) {
  console
.log(‘location change’, res) var that = this that.setData({

longitude: res.longitude,

    latitude: res.latitude,

    accuracy: res.accuracy,

    speed: res.speed

})
}

wx.onLocationChange(_locationChangeFn)
wx
.offLocationChange(_locationChangeFn) 问题: 我想获取this,然后将得到的坐标用setData的方法更新在地图上,结果拿不到this。

1 回复
xieping
xieping1 楼6 年前

定义在page里面怎么就报错了  不能用_locationChangeFn:function(res){}这样定义吗