vue使用openlayers实现移动点动画


Posted in Javascript onSeptember 24, 2020

本文实例为大家分享了vue使用openlayers实现移动点动画的具体代码,供大家参考,具体内容如下

做项目时,本来打算仿照官网的Example中动画制作,引入vue中后,发现它引用的库函数一直报错,最后我去vue中安装的依赖库中去查找这个函数,果然没有。也就是说官方例子使用的库和我安装的OL库存在一定差异。

后来我还是用笨方法去解决了,最终效果如下:

vue使用openlayers实现移动点动画

总体思路是将移动目标实例一个Overlay对象,然后将如图5个经纬度点没两点之间分割成多个(200个),之后通过定时器不断setPositon。

代码如下:

<template>
 <div>
 <div id="map"/>
 <div id="geo-marker">
  <img :src="myplanImg" >
 </div>
 </div>
</template>
<script>
// import * as myol from '@/views/openstreetmap/openlayerstools.js'
// import img from '@/assets/images'
import 'ol/ol.css'
import { Map, View, Feature } from 'ol'
import * as layer from 'ol/layer.js'
import * as source from 'ol/source.js'
import * as geom from 'ol/geom.js'
import * as style from 'ol/style.js'
import Overlay from 'ol/Overlay.js'
import TileLayer from 'ol/layer/Tile'
import { deepclone } from '@/utils/index.js'
import myplanImg from '@/../static/images/船载应急通信系统.png'
// import * as myol from '@/views/openstreetmap/animation.js'
export default {
 data() {
 return {
  // a simulated path
  path: [
  [115.6200, 14.82],
  [112.79, 14.82],
  [114.6636, 18.2977],
  [111.6870, 18.8970],
  [110.3014, 15.0630]
  ], // 模拟路径
  pathIndex: 0, // 路径点索引
  marker: null,//移动点
  splitNumber: 200, // 每两个经纬度之间的分割点
  setIntervalTime: 30, // 移动点间隔时间
  myplanImg: myplanImg, // 移动点的图片
  helpTooltipElement: null, // 平台信息div
  helpTooltip: null // 平台信息overlay
 }
 },
 created() {
 this.analysisPath(this.splitNumber)
 },
 mounted() {
 this.initSeamap()
 },
 methods: {
 initSeamap: function() {
  this.pathIndex = this.path.length - 1
  var sourceFeatures = new source.Vector()
  var layerFeatures = new layer.Vector({// 两端点Feature
  source: sourceFeatures
  })
  var lineString = new geom.LineString([])
  var layerRoute = new layer.Vector({// 两点之间的连线
  source: new source.Vector({
   features: [
   new Feature({
    geometry: lineString
   })
   ]
  }),
  style: [
   new style.Style({
   stroke: new style.Stroke({
    width: 3,
    color: 'rgba(0, 0, 0, 1)',
    lineDash: [0.1, 5]
   }),
   zIndex: 2
   })
  ],
  updateWhileAnimating: true
  })

  this.global.map = new Map({
  target: 'map',
  view: new View({
   projection: 'EPSG:4326',
   center: [109.8, 18.4],
   zoom: 7,
   minZoom: 3, // 限制最大显示
   maxZoom: 14
  }),
  layers: [
   new TileLayer({
   source: new source.OSM()
   }),
   layerRoute, layerFeatures
  ]
  })
  var markerEl = document.getElementById('geo-marker')
  markerEl.className = 'css_animation'
  this.marker = new Overlay({
  positioning: 'center-center',
  offset: [0, 0],
  element: markerEl,
  stopEvent: false
  })
  this.global.map.addOverlay(this.marker)
  var style1 = [// 开始结束点样式
  new style.Style({
   image: new style.Icon(({
   src: 'static/images/marker.png'
   }))
  })
  ]
  var feature_start = new Feature({
  geometry: new geom.Point(this.path[0])
  })
  var feature_end = new Feature({
  geometry: new geom.Point(this.path[this.path.length - 1])
  })
  feature_start.setStyle(style1)
  feature_end.setStyle(style1)
  sourceFeatures.addFeatures([feature_start, feature_end])
  lineString.setCoordinates(this.path)
  this.helpTooltipElement = document.createElement('div')
  this.helpTooltipElement.className = 'measuretip'
  this.helpTooltipElement.id = 'speed'
  this.helpTooltip = new Overlay({
  element: this.helpTooltipElement,
  offset: [15, 0],
  positioning: 'center-left'
  })
  this.global.map.addOverlay(this.helpTooltip)
  this.global.map.once('postcompose', (event) => {
  setInterval(() => {
   this.animation()
  }, this.setIntervalTime)
  })
 // this.global.map.getView().fit(lineString.getExtent())
 },
 animation: function() {
  if (this.pathIndex === -1) {
  this.pathIndex = this.path.length - 1
  }
  this.marker.setPosition(this.path[this.pathIndex])
  this.helpTooltipElement.innerHTML = '<B>名称:</B>船载应急通信系统' + '\<br\>' +
       '<B>子系统:</B>平台A,平台B' + '\<br\>' +
       '<B>经纬度:</B>' + (this.path[this.pathIndex][0] + '').substring(0, 6) + ',' +
       (this.path[this.pathIndex][1] + '').substring(0, 5)
  this.helpTooltip.setPosition(this.path[this.pathIndex])
  this.pathIndex--
 },
 analysisPath: function(splitNumber) {
  var tempPath = deepclone(this.path)
  var pathResults = []
  var tempPoint = [0, 0]
  if (tempPath.length > 1) {
  for (let i = 0; i < tempPath.length - 1; i++) { // 每两个点之间分割出splitNumber个点
   pathResults.push(tempPath[i])
   for (let j = 0; j < splitNumber; j++) {
   tempPoint[0] = (tempPath[i + 1][0] - tempPath[i ][0]) * (j + 1) / splitNumber + tempPath[i][0]
   tempPoint[1] = (tempPath[i + 1][1] - tempPath[i ][1]) * (j + 1) / splitNumber + tempPath[i][1]
   pathResults.push(deepclone(tempPoint))
   }
  }
  pathResults.push(tempPath[tempPath.length - 1])
  this.path = deepclone(pathResults)
  console.log(this.path)
  }
 }
 }

}
</script>

<style>
#map {
 width: 100%;
 height: 100%;
 overflow: hidden;
}
   .measuretip {
   position: relative;
   background-color: #0D9BF2;
   opacity: 0.7;
   border-radius: 3px;
   padding: 10px;
   font-size: 12px;
   cursor: default;
  }
</style>

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

Javascript 相关文章推荐
浅析JS刷新框架中的其他页面 &amp;&amp; JS刷新窗口方法汇总
Jul 08 Javascript
jquery sortable的拖动方法示例详解
Jan 16 Javascript
JavaScript中创建对象和继承示例解读
Feb 12 Javascript
使用JS+plupload直接批量上传图片到又拍云
Dec 01 Javascript
AngularJS学习笔记之ng-options指令
Jun 16 Javascript
JS利用cookie记忆当前位置的防刷新导航效果
Oct 15 Javascript
微信小程序canvas写字板效果及实例
Jun 15 Javascript
Vue脚手架的简单使用实例
Jul 10 Javascript
Vue高版本中一些新特性的使用详解
Sep 25 Javascript
vue工程全局设置ajax的等待动效的方法
Feb 22 Javascript
Vue动态面包屑功能的实现方法
Jul 01 Javascript
解决vue路由name同名,路由重复的问题
Aug 05 Javascript
Openlayers实现点闪烁扩散效果
Sep 24 #Javascript
基于Ionic3实现选项卡切换并重新加载echarts
Sep 24 #Javascript
vue3.0生命周期的示例代码
Sep 24 #Javascript
js 将多个对象合并成一个对象 assign方法的实现
Sep 24 #Javascript
Vue3不支持Filters过滤器的问题
Sep 24 #Javascript
jdk1.8+vue elementui实现多级菜单功能
Sep 24 #Javascript
vue实现日历表格(element-ui)
Sep 24 #Javascript
You might like
PHP函数preg_match_all正则表达式的基本使用详细解析
2013/08/31 PHP
PHP实现文件上传与下载实例与总结
2016/03/13 PHP
PHP-CGI远程代码执行漏洞分析与防范
2017/05/07 PHP
javascript 获取元素位置的快速方法 getBoundingClientRect()
2009/11/26 Javascript
javascript call方法使用说明
2010/01/11 Javascript
设为首页加入收藏兼容360/火狐/谷歌/IE等主流浏览器的代码
2013/03/26 Javascript
jQuery简单实现banner图片切换
2014/01/02 Javascript
AngularJS ng-controller 指令简单实例
2016/08/01 Javascript
javascript比较语义化版本号的实现代码
2016/09/09 Javascript
Javascript获取background属性中url的值
2016/10/17 Javascript
JS实现标签页切换效果
2017/05/04 Javascript
JS查找数组中重复元素的方法详解
2017/06/14 Javascript
详解ES6之用let声明变量以及let loop机制
2017/07/15 Javascript
vue页面跳转后返回原页面初始位置方法
2018/02/11 Javascript
聊聊JS动画库 Velocity.js的使用
2018/03/13 Javascript
vue2.0 element-ui中el-select选择器无法显示选中的内容(解决方法)
2018/08/24 Javascript
vue使用v-for实现hover点击效果
2018/09/29 Javascript
解决jquery validate 验证不通过后验证正确的信息仍残留在label上的方法
2019/08/27 jQuery
JavaScript实现网页计算器功能
2020/10/29 Javascript
[02:04]2014DOTA2国际邀请赛 DK一个时代的落幕
2014/07/21 DOTA
[00:35]2016完美“圣”典风云人物:冷冷宣传片
2016/12/08 DOTA
python3+PyQt5 创建多线程网络应用-TCP客户端和TCP服务器实例
2019/06/17 Python
Django 多对多字段的更新和插入数据实例
2020/03/31 Python
python进度条显示之tqmd模块
2020/08/22 Python
Django如何继承AbstractUser扩展字段
2020/11/27 Python
CSS3混合模式mix-blend-mode/background-blend-mode简介
2018/03/15 HTML / CSS
HTML5 与 XHTML2
2008/10/17 HTML / CSS
HTML5 canvas基本绘图之图形组合
2016/06/27 HTML / CSS
基于html5 canvas做批改作业的小插件
2020/05/20 HTML / CSS
外语系毕业生自荐信范文
2013/12/16 职场文书
应聘面试自我评价
2014/01/24 职场文书
美术毕业生求职信
2014/02/25 职场文书
2015年安置帮教工作总结
2015/05/22 职场文书
教学副校长工作总结
2015/08/13 职场文书
Springboot/Springcloud项目集成redis进行存取的过程解析
2021/12/04 Redis
Springboot-cli 开发脚手架,权限认证,附demo演示
2022/04/28 Java/Android