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


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 相关文章推荐
Javascript操纵Cookie实现购物车程序
Nov 23 Javascript
jquery 插件 人性化的消息显示
Jan 21 Javascript
JavaScript实现网站访问次数统计代码
Aug 12 Javascript
Javascript页面跳转常见实现方式汇总
Nov 28 Javascript
Bootstrap图片轮播组件使用实例解析
Jun 30 Javascript
JavaScript使用键盘输入控制实现数字验证功能
Aug 19 Javascript
jQuery实现判断控件是否显示的方法
Jan 11 Javascript
详解在React-Native中持久化redux数据
May 22 Javascript
Vue.js 实现地址管理页面思路详解(地址添加、编辑、删除和设置默认地址)
Dec 11 Javascript
angular中的post请求处理示例详解
Jun 30 Javascript
vue 动态设置img的src地址无效,npm run build 后找不到文件的解决
Jul 26 Javascript
JavaScript代码实现微博批量取消关注功能
Feb 05 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基于新浪IP库获取IP详细地址的方法
2017/05/04 PHP
PHP 应用容器化以及部署方法
2018/02/12 PHP
再论Javascript的类继承
2011/03/05 Javascript
Javascript面向对象编程(三) 非构造函数的继承
2011/08/28 Javascript
JS控制图片等比例缩放的示例代码
2013/12/24 Javascript
JS使用ajax从xml文件动态获取数据显示的方法
2015/03/24 Javascript
JavaScript实现强制重定向至HTTPS页面
2015/06/10 Javascript
省市联动效果的简单实现代码(推荐)
2016/06/06 Javascript
ionic 上拉菜单(ActionSheet)实例代码
2016/06/06 Javascript
微信小程序开发之实现自定义Toast弹框
2017/06/08 Javascript
通过cordova将vue项目打包为webapp的方法
2019/02/02 Javascript
详解BootStrap表单验证中重置BootStrap-select验证提示不清除的坑
2019/09/17 Javascript
详解JavaScript 中的批处理和缓存
2020/11/19 Javascript
11个Javascript小技巧帮你提升代码质量(小结)
2020/12/28 Javascript
[01:30:15]DOTA2-DPC中国联赛 正赛 Ehome vs Aster BO3 第二场 2月2日
2021/03/11 DOTA
python开发之函数定义实例分析
2015/11/12 Python
Python使用正则表达式实现文本替换的方法
2017/04/18 Python
Python中的groupby分组功能的实例代码
2018/07/11 Python
Python运维开发之psutil库的使用详解
2018/10/18 Python
Python Lambda函数使用总结详解
2019/12/11 Python
Python 2种方法求某个范围内的所有素数(质数)
2020/01/31 Python
Python使用OpenPyXL处理Excel表格
2020/07/02 Python
世界上最大的家庭自动化公司:Smarthome
2017/12/20 全球购物
Tomcat Mysql datasource数据源配置
2015/12/28 面试题
团员的自我评价
2013/12/01 职场文书
资产经营总监岗位职责范文
2013/12/01 职场文书
小班评语大全
2014/05/04 职场文书
小学运动会班级口号
2014/06/09 职场文书
课外小组活动总结
2014/08/27 职场文书
检讨书1000字
2014/10/11 职场文书
2015年评职称工作总结范文
2015/04/20 职场文书
2015财务年度工作总结范文
2015/05/04 职场文书
个人合作协议范本
2015/08/06 职场文书
vue3获取当前路由地址
2022/02/18 Vue.js
Oracle使用别名的好处
2022/04/19 Oracle
苹果macOS 13开发者预览版Beta 8发布 正式版10月发布
2022/09/23 数码科技