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


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对象的构造和继承实现代码
Dec 05 Javascript
几种设置表单元素中文本输入框不可编辑的方法总结
Nov 25 Javascript
JavaScript实现两个Table固定表头根据页面大小自行调整
Jan 03 Javascript
jQuery中的val()示例应用
Feb 26 Javascript
JQuery ztree 异步加载实例讲解
Feb 25 Javascript
基于JS实现导航条flash导航条
Jun 17 Javascript
jQuery基于函数重载实现自定义Alert函数样式的方法
Jul 27 Javascript
js面向对象编程总结
Feb 16 Javascript
Vue.js组件间的循环引用方法示例
Dec 27 Javascript
vue中使用百度脑图kityminder-core二次开发的实现
Sep 26 Javascript
浅析JS中NEW的实现原理及重写
Feb 20 Javascript
学习 Vue.js 遇到的那些坑
Feb 02 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导出Excel实例讲解
2016/01/24 PHP
一个可以兼容IE FF的加为首页与加入收藏实现代码
2009/11/02 Javascript
JQuery 将元素显示在屏幕的中央的代码
2010/02/27 Javascript
js使用栈来实现10进制转8进制与取除数及余数
2014/06/11 Javascript
Jquery选择器中使用变量实现动态选择例子
2014/07/25 Javascript
分享9点个人认为比较重要的javascript 编程技巧
2015/04/27 Javascript
如何使用jquery easyui创建标签组件
2015/11/18 Javascript
深入理解JS DOM事件机制
2016/08/06 Javascript
利用vue实现模态框组件
2016/12/19 Javascript
Javascript 两种刷新方法以及区别和适用范围
2017/01/17 Javascript
详解angular用$sce服务来过滤HTML标签
2017/04/11 Javascript
详解webpack介绍&amp;安装&amp;常用命令
2017/06/29 Javascript
对于input 框限定输入值为浮点型的js代码
2017/09/25 Javascript
微信小程序实现页面跳转传值的方法
2017/10/12 Javascript
快速解决brew安装特定版本flow的问题
2018/05/17 Javascript
webstorm+vue初始化项目的方法
2018/10/18 Javascript
详解Express笔记之动态渲染HTML(新手入坑)
2018/12/13 Javascript
解决vue+ element ui 表单验证有值但验证失败问题
2020/01/16 Javascript
node.JS路径解析之PATH模块使用方法详解
2020/02/06 Javascript
使用apidoc管理RESTful风格Flask项目接口文档方法
2018/02/07 Python
python使用mysql的两种使用方式
2018/03/07 Python
关于Python的一些学习总结
2018/05/25 Python
Django项目中包含多个应用时对url的配置方法
2018/05/30 Python
pycharm运行和调试不显示结果的解决方法
2018/11/30 Python
Python单元测试unittest的具体使用示例
2018/12/17 Python
Python实现RGB与HSI颜色空间的互换方式
2019/11/27 Python
keras用auc做metrics以及早停实例
2020/07/02 Python
如何在VSCode下使用Jupyter的教程详解
2020/07/13 Python
python openpyxl模块的使用详解
2021/02/25 Python
python matplotlib工具栏源码探析二之添加、删除内置工具项的案例
2021/02/25 Python
英国和国际包裹递送:ParcelCompare
2019/08/26 全球购物
二年级评语大全
2014/04/23 职场文书
银行党的群众路线教育实践活动对照检查材料
2014/09/25 职场文书
领导欢送会主持词
2015/07/06 职场文书
实现一个简单得数据响应系统
2021/11/11 Javascript
使用PostGIS完成两点间的河流轨迹及流经长度的计算(推荐)
2022/01/18 PostgreSQL