微信小程序实现轨迹回放的示例代码


Posted in Javascript onDecember 13, 2019

在微信小程序实现轨迹回放的效果

1、wxml

<map id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="16"
 bindcontroltap="controltap" bindmarkertap="markertap" markers="{{markers}}" polyline="{{polyline}}"
 bindregionchange="regionchange" show-location style="width: 100%; height:{{height}}px;" ></map>

 <view class="padding flex flex-wrap justify-between align-center bg-white">
  <button class='cu-btn bg-green shadow sm' bindtap='beginTrack'> 开始 </button>
  <button class='cu-btn bg-orange shadow sm' bindtap='pauseTrack'> 暂停 </button>
  <button class='cu-btn bg-red shadow sm' bindtap='endTrack'> 结束 </button>
 </view>

2、js

//index.js
//获取应用实例
const app = getApp()

Page({
 data: {
  StatusBar: app.globalData.StatusBar,
  CustomBar: app.globalData.CustomBar,
  height: wx.getSystemInfoSync().windowHeight,
  latitude: 0,
  longitude: 0,
  playIndex: 0,  
  timer: null,
  markers: [],
  polyline: [],
  pointsInfo:[]
 },
 regionchange(e) {
  //console.log(e.type)
 },
 markertap(e) {
  //console.log(e.markerId)
 },
 controltap(e) {
  //console.log(e.controlId)
 },
 beginTrack:function(e){

 },
 onLoad: function (options){
  var that = this;
  wx.request({
   url: 'http://**/getTrack',
   data: { 
    beginTime:"开始时间",
    endTime:"结束时间"
   },
   method: "post",
   success: function (res) {
    that.setData({
     pointsInfo:res.data.pointsInfos,
     polyline: [{
      points: res.data.points,
      color: "#FF0000DD",
      width: 4,
      dottedLine: true
     }],
     markers: [{
      iconPath: '../../img/location.jpg',
      id: 0,
      latitude: res.data.points[0].latitude,
      longitude: res.data.points[0].longitude,
      width: 30,
      height: 30,
      title: that.data.brandNumber,
      callout: {
       content: that.data.brandNumber + ' \n 时间:' + res.data.pointsInfos[0].create_time + ' \n 速度:' + res.data.pointsInfos[0].speed + ' km/h',
       color: "#000000",
       fontSize: 13,
       borderRadius: 2,
       bgColor: "#fff",
       display: "ALWAYS",
       boxShadow: "5px 5px 10px #aaa"
      }
     }],
     latitude: res.data.points[0].latitude,
     longitude: res.data.points[0].longitude,
    })
   }
  })
 },
 /**
  * 开始
  */
 beginTrack:function(){
  var that = this;
  var i = that.data.playIndex == 0 ? 0 : that.data.playIndex;
  that.timer = setInterval(function () {
   i ++
   that.setData({
    playIndex: i,
    latitude: that.data.polyline[0].points[i].latitude,
    longitude: that.data.polyline[0].points[i].longitude,
    markers: [{
     iconPath: '../../img/car/e0.png',
     id: 0,
     latitude: that.data.polyline[0].points[i].latitude,
     longitude: that.data.polyline[0].points[i].longitude,
     width: 30,
     height: 30,
     title: that.data.brandNumber,
     callout: {
      content: that.data.brandNumber + ' \n 时间:' + that.data.pointsInfo[i].create_time + ' \n 速度:' + that.data.pointsInfo[i].speed + ' km/h',
      color: "#000000",
      fontSize: 13,
      borderRadius: 2,
      bgColor: "#fff",
      display: "ALWAYS",
      boxShadow: "5px 5px 10px #aaa"
     }
    }]
   }) 
   if ((i+1) >= that.data.polyline[0].points.length) { 
    that.endTrack();
   }
  }, 500) 
 }, 
 /**
  * 暂停
  */
 pauseTrack:function(){
  var that = this; 
  clearInterval(this.timer)
 },
 /**
  * 结束
  */
 endTrack:function(){
  var that = this; 
  that.setData({
   playIndex: 0,
   latitude: that.data.polyline[0].points[0].latitude,
   longitude: that.data.polyline[0].points[0].longitude,
   markers: [{
    iconPath: '../../img/car/e0.png',
    id: 0,
    latitude: that.data.polyline[0].points[0].latitude,
    longitude: that.data.polyline[0].points[0].longitude,
    width: 30,
    height: 30,
    title: that.data.brandNumber,
    callout: {
     content: that.data.brandNumber + ' \n 时间:' + that.data.pointsInfo[0].create_time + ' \n 速度:' + that.data.pointsInfo[0].speed + ' km/h',
     color: "#000000",
     fontSize: 13,
     borderRadius: 2,
     bgColor: "#fff",
     display: "ALWAYS",
     boxShadow: "5px 5px 10px #aaa"
    }
   }]
  }) 
  clearInterval(this.timer)
 }
})

后台数据使用的是百度鹰眼的数据。最终效果:

微信小程序实现轨迹回放的示例代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
HTML代码中标签的全部属性 中文注释说明
Mar 26 Javascript
FileUpload 控件 禁止手动输入或粘贴的实现代码
Apr 07 Javascript
基于jquery的jqDnR拖拽溢出的修改
Feb 12 Javascript
javascript中的数字与字符串相加实例分析
Aug 14 Javascript
ExtJs纵坐标值重复问题的解决方法
Feb 27 Javascript
浅谈javascript中的闭包
May 13 Javascript
jquery移动端TAB触屏切换实现效果
Dec 22 Javascript
javascript使用btoa和atob来进行Base64转码和解码
Mar 20 Javascript
webpack配置导致字体图标无法显示的解决方法
Mar 06 Javascript
纯 JS 实现放大缩小拖拽功能(完整代码)
Nov 25 Javascript
微信小程序实现watch监听
Jun 04 Javascript
antd Form组件方法getFieldsValue获取自定义组件的值操作
Oct 29 Javascript
微信小程序 SOTER 生物认证DEMO 指纹识别功能
Dec 13 #Javascript
vue中使用elementUI组件手动上传图片功能
Dec 13 #Javascript
使用uni-app开发微信小程序的实现
Dec 13 #Javascript
webpack DllPlugin xxx is not defined解决办法
Dec 13 #Javascript
微信小程序关键字变色实现代码实例
Dec 13 #Javascript
Servlet返回的数据js解析2种方法
Dec 12 #Javascript
微信小程序实现横向滚动导航栏效果
Dec 12 #Javascript
You might like
PHP整合PayPal支付
2015/06/11 PHP
深入浅析php json 格式控制
2015/12/24 PHP
PHP中include/require/include_once/require_once使用心得
2016/08/28 PHP
js动态添加表格数据使用insertRow和insertCell实现
2014/05/22 Javascript
jQuery圆形统计图开发实例
2015/01/04 Javascript
jquery实现简易的移动端验证表单
2015/11/08 Javascript
详解JavaScript对象序列化
2016/01/19 Javascript
js下将金额数字每三位一逗号分隔
2016/02/19 Javascript
jQuery height()、innerHeight()、outerHeight()函数的区别详解
2016/05/23 Javascript
全面了解JavaScirpt 的垃圾(garbage collection)回收机制
2016/07/11 Javascript
微信小程序-消息提示框实例
2016/11/24 Javascript
Ajax实现不刷新取最新商品
2017/03/01 Javascript
JS中使用正则表达式g模式和非g模式的区别
2017/04/01 Javascript
一篇看懂vuejs的状态管理神器 vuex状态管理模式
2017/04/20 Javascript
JavaScript作用域、闭包、对象与原型链概念及用法实例总结
2018/08/20 Javascript
vue实现的网易云音乐在线播放和下载功能案例
2019/02/18 Javascript
webpack4 optimization使用总结
2019/11/10 Javascript
React中使用UMEditor的方法示例
2019/12/27 Javascript
详解React 条件渲染
2020/07/08 Javascript
Python实现命令行通讯录实例教程
2016/08/18 Python
Python冒泡排序注意要点实例详解
2016/09/09 Python
解决python给列表里添加字典时被最后一个覆盖的问题
2019/01/21 Python
Python的matplotlib绘图如何修改背景颜色的实现
2019/07/16 Python
Python爬取豆瓣视频信息代码实例
2019/11/16 Python
基于python操作ES实例详解
2019/11/16 Python
python爬虫开发之urllib模块详细使用方法与实例全解
2020/03/09 Python
python 工具 字符串转numpy浮点数组的实现
2020/03/14 Python
使用Keras中的ImageDataGenerator进行批次读图方式
2020/06/17 Python
Pandas之缺失数据的实现
2021/01/06 Python
社区志愿者活动总结
2014/06/26 职场文书
人口与计划生育目标管理责任书
2014/07/29 职场文书
诚信承诺书
2015/01/19 职场文书
考教师资格证不要错过的4个最佳时机
2019/07/17 职场文书
百善孝为先:关于孝道的经典语录
2019/10/18 职场文书
Mysql 8.x 创建用户以及授予权限的操作记录
2022/04/18 MySQL