首先用到的API有两个
wx.startLocationUpdate 开启实时定位 再通过onLocationChange进行监听就可以实现实时定位功能
wx.startLocationUpdate({
success: (res) => {
wx.onLocationChange((data) => {
//获取当前时间
var currentTime = new Date().getTime();
//获取上次保存的位置信息
var oldLocation = wx.getStorageSync('oldLocation');
//获取上次执行的时间
var oldTime = wx.getStorageSync('oldTime');
//将经纬度拼接
var newLocation = data.latitude+""+data.longitude;
//判断当前的位置是否和上次位置不一致,并且间隔时间超过5s
if(oldLocation!=newLocation && currentTime-oldTime>5000){
//缓存当前最新位置
wx.setStorageSync('oldLocation',newLocation);
//缓存当前执行的时间
wx.setStorageSync('oldTime',currentTime);
//将位置信息上传后台的自己的代码
uploadLocation(data);
}
});
},
fail: (err) => {
consoler.log(err);
}
})