openlayers 3实现车辆轨迹回放


Posted in Javascript onSeptember 24, 2020

本文实例为大家分享了openlayers 3实现车辆轨迹回放的具体代码,供大家参考,具体内容如下

先上效果:

openlayers 3实现车辆轨迹回放

利用 openlayers 3地图的 postcompose 事件监听地图的重绘

注意:此代码是我在Vue 的methods 里面写的测试方法,并不能直接运行,请在理解的基础上测试。
vm 为vue的this对象 注释已经很丰富了,先做个备份,后期会编辑加入一点详解。

实现代码:

html:

<div id="menu">
   <label for="speed" style="font-weight: bold;">
    运动速度: 
    <input id="speed" type="range" min="1" max="20" step="1" value="10" />
   </label>
   <button id="start-animation">
    开始运动
   </button>
  </div>
<!-- 注:此代码仅为上面速度条和按钮--

核心代码:

startMove:function () {
    var vm=this;
    var map=vm.map;
    vm.clearOverlayers("beijing_sq");

    //中间站
    var stops=[
     [12909554.6597,4933234.84552], //14
     [12909824.6852,4931594.7854], //43
     [12910026.8837,4930523.89946], //63
     [12910870.563,4929357.26511]  //85
    ];

    var stopMakers = new Array();

    for(var i=0;i<4;i++){
     var s = new ol.Feature({
      type: 'stop',
      geometry: new ol.geom.Point(stops[i])
     });
     stopMakers.push(s);
    }


    var Coordinates=vm.path;

    //将离散点构建成一条折线
    var route = new ol.geom.LineString(Coordinates);
    //获取直线的坐标
    var routeCoords = route.getCoordinates();
    var routeLength = routeCoords.length;

    var routeFeature = new ol.Feature({
     type: 'route',
     geometry: route
    });
    var geoMarker = new ol.Feature({
     type: 'geoMarker',
     geometry: new ol.geom.Point(routeCoords[0])
    });
    var startMarker = new ol.Feature({
     type: 'icon',
     geometry: new ol.geom.Point(routeCoords[0])
    });
    var endMarker = new ol.Feature({
     type: 'icon',
     geometry: new ol.geom.Point(routeCoords[routeLength - 1])
    });

    var styles = {
     'route': new ol.style.Style({
      stroke: new ol.style.Stroke({
       width: 6,
       color: '#F2C841'
      }),
      fill:new ol.style.Fill({
       color:"#F6E3A3"
      })
     }),
     /*'icon': new ol.style.Style({
      image: new ol.style.Icon({
       anchor: [0.5, 1],
       src: require()
      })
     }),*/
     'geoMarker': new ol.style.Style({
       /*image: new ol.style.Circle({
        radius: 7,
        snapToPixel: false,
        fill: new ol.style.Fill({ color: 'black' }),
        stroke: new ol.style.Stroke({
         color: 'white',
         width: 2
        })
       })*/
       image: new ol.style.Icon({
        src: require('../../assets/map/left_red_car.png'),
        rotateWithView: false,
        rotation: -Math.atan2(routeCoords[0][1]-routeCoords[1][1], routeCoords[0][0]-routeCoords[1][0]),
        scale:0.3,
       })
      }),
     'stop':new ol.style.Style({
      image:new ol.style.Circle({
       radius:10,
       snapToPixel:false,
       fill:new ol.style.Fill({ color:'red'}),
       stroke:new ol.style.Stroke({
        color:'white',
        width:2
       })
      })
     })
    };

    var animating = false;
    var speed, now;
    var speedInput = document.getElementById('speed');
    var startButton = document.getElementById('start-animation');

    var vectorLayer = new ol.layer.Vector({
     id:'carLayer',
     source: new ol.source.Vector({
      features: [routeFeature, geoMarker, startMarker, endMarker,stopMakers[0],stopMakers[1],stopMakers[2],stopMakers[3]]
     }),
     style: function (feature) {
      //如果动画是激活的就隐藏geoMarker
      if (animating && feature.get('type') === 'geoMarker') {
       return null;
      }
      return styles[feature.get('type')];
     }
    });

    //var center = ol.proj.fromLonLat([115.981,40.451]);

    map.addLayer(vectorLayer);

    // 要素移动
    var moveFeature = function (event) {
     var vectorContext = event.vectorContext; //HTML5 Canvas context,ol.render.canvas.Immediate的对象
     var frameState = event.frameState;  //freme 的状态
     if (animating) {
      var elapsedTime = frameState.time - now; //elapsedTime 已过时间
      //通过增加速度,来获得lineString坐标
      var index = Math.round(speed * elapsedTime / 1000); //已经走了多少个点

      //console.log("#########",routeCoords[index]);

      if (index >= routeLength) {
       stopAnimation(true);
       return;
      }

      //fixme ---------------
      if( index < 14){
       flashFeature(0);
      }
      if( index == 14){
       changeStyle(0, 1);
      }

      if(index > 14 && index <43){
       flashFeature(1);
      }
      if(index == 43){
       changeStyle(1, 2);
      }


      if(index > 43 && index <63){
       flashFeature(2);
      }
      if(index == 63){
       changeStyle(2, 3);
      }

      if(index > 63 && index <85){
       flashFeature(3);
      }
      if(index == 85){
       changeStyle(3, 3);
      }
      //fixme--------------------

      var dx,dy,rotation,carStyle;
      if(routeCoords[index] && routeCoords[index+1]){
       dx=routeCoords[index][0]-routeCoords[index+1][0];
       dy=routeCoords[index][1]-routeCoords[index+1][1];
       rotation = Math.atan2(dy,dx);
       //console.log("***********",rotation);

       carStyle= new ol.style.Style({
        image: new ol.style.Icon({
         src: require('../../assets/map/left_red_car.png'),
         rotateWithView: false,
         rotation: -rotation,
         scale:0.3,
        })
       });
       var currentPoint = new ol.geom.Point(routeCoords[index]); //当前点
       var feature = new ol.Feature(currentPoint);
       //Render a feature into the canvas.
       // Note that any zIndex on the provided style will be ignored - features are rendered immediately in the order that this method is called.
       // If you need zIndex support, you should be using an ol.layer.Vector instead
       vectorContext.drawFeature(feature, carStyle);
      }
     }
     //继续动画效果
     map.render();
    };

    function changeStyle(previous,next) {
     //console.log(stopMakers[previous]);
     stopMakers[previous].setStyle(new ol.style.Style({
      image: new ol.style.Circle({
       radius: 10,
       snapToPixel: false,
       fill: new ol.style.Fill({color: 'green'}),
       stroke: new ol.style.Stroke({
        color: 'white',
        width: 2
       })
      })
     }));
    }

    var colors=['red','green'];
    var colorIndex=0;
    function flashFeature(index) {
     var color;
     colorIndex++;
     colorIndex=colorIndex % 30;

     if(colorIndex < 15){
      color=colors[0];
     }else {
      color = colors[1];
     }
     stopMakers[index].setStyle(new ol.style.Style({
      image: new ol.style.Circle({
       radius: 10,
       snapToPixel: false,
       fill: new ol.style.Fill({
        color: color
       }),
       stroke: new ol.style.Stroke({
        color: 'white',
        width: 2
       })
      })
     }));
    }

    function startAnimation() {
     if (animating) {
      stopAnimation(false);
     } else {
      animating = true;
      now = new Date().getTime();   /** 开始时的时间*/
      speed = speedInput.value;
      startButton.textContent = '结束运动';
      //隐藏geoMarker
      geoMarker.setStyle(null);
      //设置显示范围
      //map.getView().setCenter(center);
      map.on('postcompose', moveFeature); /** postcompose事件-- 地图渲染时都会触发 */
      map.render();
     }
    }

    /**
     * @param {boolean}结束动画
     */
    function stopAnimation(ended) {
     animating = false;
     startButton.textContent = '开始运动';

     //如果动画取消就开始动画
     var coord = ended ? routeCoords[routeLength - 1] : routeCoords[0];
     /** @type {ol.geom.Point} */
     (geoMarker.getGeometry()).setCoordinates(coord);
     //移除监听
     map.un('postcompose', moveFeature); /** 解除postcompose 事件 */
    }

    startButton.addEventListener('click', startAnimation, false);
}

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

Javascript 相关文章推荐
键盘控制事件应用教程大全
Nov 24 Javascript
js自动查找select下拉的菜单并选择(示例代码)
Feb 26 Javascript
Javascript学习笔记之数组的构造函数
Nov 23 Javascript
JQuery异步加载PartialView的方法
Jun 07 Javascript
Knockoutjs 学习系列(二)花式捆绑
Jun 07 Javascript
快速解决js动态改变dom元素属性后页面及时渲染的问题
Jul 06 Javascript
浅谈angular懒加载的一些坑
Aug 20 Javascript
vue.js初学入门教程(1)
Nov 03 Javascript
基于Vue单文件组件详解
Sep 15 Javascript
解决Angular4项目部署到服务器上刷新404的问题
Aug 31 Javascript
浅谈Webpack4 Tree Shaking 终极优化指南
Nov 18 Javascript
如何使用vue3打造一个物料库
May 08 Vue.js
vue-openlayers实现地图坐标弹框效果
Sep 24 #Javascript
vue集成openlayers加载geojson并实现点击弹窗教程
Sep 24 #Javascript
Vue+Openlayers自定义轨迹动画
Sep 24 #Javascript
vue使用openlayers实现移动点动画
Sep 24 #Javascript
Openlayers实现点闪烁扩散效果
Sep 24 #Javascript
基于Ionic3实现选项卡切换并重新加载echarts
Sep 24 #Javascript
vue3.0生命周期的示例代码
Sep 24 #Javascript
You might like
Discuz!下Memcache缓存实现方法
2010/05/28 PHP
PHP和Shell实现检查SAMBA与NFS Server是否存在
2015/01/07 PHP
PHP中session跨子域的三种实现方法
2016/07/25 PHP
PHP实现验证码校验功能
2017/11/16 PHP
php封装的page分页类完整实例代码
2020/02/01 PHP
jQuery使用手册之一
2007/03/24 Javascript
jQuery使用手册之 事件处理
2007/03/24 Javascript
JS正则中的RegExp对象对象
2012/11/07 Javascript
jquery延迟加载外部js实现代码
2013/01/11 Javascript
jquery实现一个简单好用的弹出框
2014/09/26 Javascript
Node.js中JavaScript操作MySQL的常用方法整理
2016/03/01 Javascript
原生js三级联动的简单实现代码
2016/06/07 Javascript
三种带箭头提示框总结实例
2016/06/14 Javascript
JavaScript中全选、全不选、反选、无刷新删除、批量删除、即点即改入库(在yii框架中操作)的代码分享
2016/11/01 Javascript
详解webpack进阶之插件篇
2017/07/06 Javascript
vue-router 导航钩子的具体使用方法
2017/08/31 Javascript
react-native 完整实现登录功能的示例代码
2017/09/11 Javascript
js实现会跳动的日历效果(完整实例)
2017/10/18 Javascript
基于jQuery中ajax的相关方法汇总(必看篇)
2017/11/08 jQuery
微信小程序开发注意指南和优化实践(小结)
2019/06/21 Javascript
世界上最短的数字判断js代码
2019/09/09 Javascript
[05:29]2014DOTA2国际邀请赛 赛后专访:LGDNewbee顺利过关
2014/07/13 DOTA
[01:01:22]VGJ.S vs OG 2018国际邀请赛淘汰赛BO3 第一场 8.22
2018/08/23 DOTA
Python sqlite3事务处理方法实例分析
2017/06/19 Python
Python 给下载文件显示进度条和下载时间的实现
2020/04/02 Python
Python使用pyexecjs代码案例解析
2020/07/13 Python
python 如何利用argparse解析命令行参数
2020/09/11 Python
pycharm中如何自定义设置通过“ctrl+滚轮”进行放大和缩小实现方法
2020/09/16 Python
Django用户认证系统如何实现自定义
2020/11/12 Python
中队活动总结
2014/08/27 职场文书
骨干教师申报材料
2014/12/17 职场文书
任命书标准格式
2015/03/02 职场文书
道歉的话语大全
2015/05/12 职场文书
2019最新企业员工考勤管理制度(通用版)!
2019/07/02 职场文书
python实现监听键盘
2021/04/26 Python
《游戏王:大师决斗》将推出新卡牌包4月4日上线
2022/03/31 其他游戏