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 相关文章推荐
Opacity.js
Jan 22 Javascript
js 新浪的一个图片播放图片轮换效果代码
Jul 15 Javascript
var与Javascript变量隐式声明
Sep 17 Javascript
javascript制作游戏开发碰撞检测的封装代码
Mar 31 Javascript
jQuery3.0中的buildFragment私有函数详解
Aug 16 Javascript
浅谈js算法和流程控制
Dec 29 Javascript
关于Bootstrap按钮组件消除黄框的方法
May 19 Javascript
JavaScript 中定义函数用 var foo = function () {} 和 function foo()区别介绍
Mar 01 Javascript
微信小程序云开发(数据库)详解
May 17 Javascript
小程序接口的promise化的实现方法
Dec 11 Javascript
关于vue 结合原生js 解决echarts resize问题
Jul 26 Javascript
JS实现选项卡插件的两种写法(jQuery和class)
Dec 30 jQuery
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中json_encode中文编码问题分析
2011/09/13 PHP
laravel框架中视图的基本使用方法分析
2019/11/23 PHP
HTTP头隐藏PHP版本号实现过程解析
2020/12/09 PHP
BOOM vs RR BO3 第二场2.13
2021/03/10 DOTA
一些易混淆且不常用的属性,希望有用
2007/01/29 Javascript
AngularJS 面试题集锦
2016/09/06 Javascript
详解前后端分离之VueJS前端
2017/05/24 Javascript
详解nodejs异步I/O和事件循环
2017/06/07 NodeJs
vue resource post请求时遇到的坑
2017/10/19 Javascript
VUEJS 2.0 子组件访问/调用父组件的实例
2018/02/10 Javascript
Angular利用内容投射向组件输入ngForOf模板的方法
2018/03/05 Javascript
js+canvas实现验证码功能
2020/09/21 Javascript
学习使用ExpressJS 4.0中的新Router的用法
2018/11/06 Javascript
javascript自定义日期比较函数用法示例
2019/07/22 Javascript
vue中使用[provide/inject]实现页面reload的方法
2019/09/30 Javascript
详解vue中使用axios对同一个接口连续请求导致返回数据混乱的问题
2019/11/06 Javascript
利用Python进行数据可视化常见的9种方法!超实用!
2018/07/11 Python
使用TensorFlow实现SVM
2018/09/06 Python
为什么str(float)在Python 3中比Python 2返回更多的数字
2018/10/16 Python
Python 中导入csv数据的三种方法
2018/11/01 Python
Python序列对象与String类型内置方法详解
2019/10/22 Python
Python修改列表值问题解决方案
2020/03/06 Python
Python urlopen()参数代码示例解析
2020/12/10 Python
全球最大的网上自行车商店:Chain Reaction Cycles
2016/12/02 全球购物
水上运动奥特莱斯:Wasterports Outlet
2018/08/08 全球购物
经典C++面试题一
2016/11/06 面试题
班级聚会策划书
2014/01/16 职场文书
大专毕业自我鉴定
2014/02/04 职场文书
《我的第一本书》教学反思
2014/02/15 职场文书
企业宣传策划方案
2014/05/29 职场文书
2015年服务员工作总结
2015/04/08 职场文书
2015年暑期社会实践方案
2015/07/14 职场文书
《蜜蜂引路》教学反思
2016/02/22 职场文书
Python下opencv使用hough变换检测直线与圆
2021/06/18 Python
Redis之RedisTemplate配置方式(序列和反序列化)
2022/03/13 Redis
Nginx+Windows搭建域名访问环境的操作方法
2022/03/17 Servers