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


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 相关文章推荐
js判断设备是否为PC并调整图片大小
Feb 12 Javascript
javascript实现漂亮的拖动层,窗口拖拽特效
Apr 24 Javascript
Ajax中解析Json的两种方法对比分析
Jun 25 Javascript
全面解析Bootstrap排版使用方法(标题)
Nov 30 Javascript
聊一聊jQuery插件uploadify使用方法
Aug 24 Javascript
微信小程序 登录实例详解
Jan 16 Javascript
angular-cli修改端口号【angular2】
Apr 19 Javascript
Angular4.x Event (DOM事件和自定义事件详解)
Oct 09 Javascript
js面试题之异步问题的深入理解
Sep 20 Javascript
Angular短信模板校验代码
Sep 23 Javascript
js实现随机圆与矩形功能
Oct 29 Javascript
Vue与React的区别和优势对比
Dec 18 Vue.js
微信小程序 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+mysql留言本源码
2009/11/11 PHP
php+redis实现多台服务器内网存储session并读取示例
2017/01/12 PHP
php微信开发之图片回复功能
2018/06/14 PHP
Hutia 的 JS 代码集
2006/10/24 Javascript
改善用户体验的五款jQuery插件分享
2011/05/22 Javascript
jquery 操作日期、星期、元素的追加的实现代码
2012/02/07 Javascript
js创建元素(节点)示例
2014/01/02 Javascript
Ext修改GridPanel数据和字体颜色、css属性等
2014/06/13 Javascript
jQuery中unbind()方法用法实例
2015/01/19 Javascript
js入门之Function函数的使用方法【新手必看】
2016/11/22 Javascript
微信小程序 仿猫眼实现实例代码
2017/03/14 Javascript
JS排序之快速排序详解
2017/04/08 Javascript
基于jQuery实现瀑布流页面
2017/04/11 jQuery
Javascript创建类和对象详解
2017/05/31 Javascript
vue中使用cropperjs的方法
2018/03/01 Javascript
分享5个顶级的JavaScript Ajax组件库
2018/09/16 Javascript
使用JS获取页面上的所有标签
2018/10/18 Javascript
Vue 实现前进刷新后退不刷新的效果
2019/06/14 Javascript
Vue开发中常见的套路和技巧总结
2020/11/24 Vue.js
[48:44]2014 DOTA2国际邀请赛中国区预选赛5.21 TongFu VS HGT
2014/05/22 DOTA
[01:54]胎教DOTA2 准妈妈玩家现身中国区预选赛
2016/06/26 DOTA
对Python新手编程过程中如何规避一些常见问题的建议
2015/04/01 Python
Python线程详解
2015/06/24 Python
python编程嵌套函数实例代码
2018/02/11 Python
python3中os.path模块下常用的用法总结【推荐】
2018/09/16 Python
python装饰器简介---这一篇也许就够了(推荐)
2019/04/01 Python
python交易记录整合交易类详解
2019/07/03 Python
Python如何使用input函数获取输入
2020/08/06 Python
python time.strptime格式化实例详解
2021/02/03 Python
估算杭州有多少软件工程师
2015/08/11 面试题
电气工程师岗位职责
2014/01/01 职场文书
优秀毕业生推荐信范文
2014/03/07 职场文书
岗位竞聘演讲稿范文
2014/04/24 职场文书
大学社团计划书
2014/05/01 职场文书
单方离婚协议书范本2014
2014/10/28 职场文书
Nginx stream 配置代理(Nginx TCP/UDP 负载均衡)
2021/11/17 Servers