const APP = getApp(); let mapContext; //地图对象 let clockRefresh = null; let continueRefresh = null; Page({ data: { device: null, //设备信息 location: null, //位置信息 mapCenter: { //地图聚焦中心 默认北京 latitude: 39.9, longitude: 116.6, }, markers: [], //地图标记 }, // 监听页面加载 onLoad: function () { // 获取地图对象 mapContext = wx.createMapContext('map'); this.setData({ device: APP.getCurrentDevice() }) }, // 监听页面显示 onShow: function () { const that = this; if (APP.getDeviceList().length < 1) { APP.Modal.tips("您还没有设备可以查看"); mapContext.moveToLocation({}); return; } //请求设备位置 this.getLocation(APP.getCurrentDevice().deviceId, false); setTimeout(function () { that.intervalRefresh(APP.getCurrentDevice().deviceId); }, 5000) that.intervalSetContinue(APP.getCurrentDevice().deviceId); }, // 监听页面隐藏 onHide: function () { //清除定时 clearInterval(clockRefresh); clearInterval(continueRefresh); clockRefresh = null; continueRefresh = null; }, /** * 定时刷新 */ intervalRefresh: function (deviceId) { console.log("启动") const that = this; if (clockRefresh != null) return; clockRefresh = setInterval(function () { that.getLocationInterval(deviceId); }, 15000) }, /** * 定时设置持续定位 */ intervalSetContinue: function (deviceId) { console.log("启动持续定位") const that = this; if (continueRefresh != null) return; that.setContinue(deviceId); continueRefresh = setInterval(function () { that.setContinue(deviceId); }, 240000) }, /** * 设置持续定位 */ setContinue: function (deviceId) { const params = { deviceId: deviceId, highFreq: 1 } APP.Post(APP.Url.setContinue, params).then(res => {}).catch(res => { console.log(res) }) }, /** * 聚焦中心 */ center: function () { if (this.data.location == null) return; this.moveTo(this.data.location.lonGcj, this.data.location.latGcj); }, /** * 获取设备位置 */ getLocation: function (deviceId) { const that = this; const params = { deviceId: deviceId } APP.Get(APP.Url.getLocation, params, false).then(res => { const location = res.data; if (location == null) { APP.Modal.tips("该设备还没有上传位置信息"); return; } //首次加载聚焦中心为当前位置 that.moveTo(location.lonGcj, location.latGcj); that.setLocation(location); }) }, /** * 获取设备位置 * 定时器 */ getLocationInterval: function (deviceId) { console.log("请求数据") const params = { deviceId: deviceId } APP.Get(APP.Url.getLocation, params, false).then(res => { this.setLocation(res.data); }) }, /** * 设置位置信息 */ setLocation: function (location) { if (location == null) return; //信号强度 location.signal = this.formatSignal(location.signalnum); //定位时间 location.latestTime = this.formatTime(location.uploadTime) this.setData({ location: location }) //添加地图标记 this.addMarker(location.lonGcj, location.latGcj) }, /** * 格式化时间显示 */ formatTime: function (date) { const diff = (new Date().getTime() - new Date(date).getTime()) / 1000; if (diff < 60) return "刚刚"; if (diff < 3600) return parseInt(diff / 60) + "分钟前"; if (diff < 86400) return parseInt(diff / 3600) + "小时前"; if (diff < 31104000) return parseInt(diff / 86400) + "天前"; return "1年前"; }, /** * 格式化电池信号量 */ formatSignal: function (num) { if (num > 90) return "信号良好"; if (num > 60) return "信号正常"; if (num > 30) return "信号较差"; if (num == 0) return "无信号"; return "信号差" }, /** * 添加标记 */ addMarker: function (lng, lat) { this.setData({ markers: [{ id: 1, iconPath: "/imgs/marker.png", latitude: parseFloat(lat), longitude: parseFloat(lng), width: 35, height: 35 }] }) }, /** * 移动地图焦点到某个点 */ moveTo: function (lng, lat) { mapContext.moveToLocation({ latitude: parseFloat(lat), //纬度 longitude: parseFloat(lng), //经度 }) } })