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一组验证函数
Dec 20 Javascript
jQuery EasyUI API 中文文档 DateTimeBox日期时间框
Oct 16 Javascript
javascript对talbe进行动态添加、删除、验证实现代码
Mar 29 Javascript
Android中资源文件(非代码部分)的使用概览
Dec 18 Javascript
JavaScript设计模式之适配器模式介绍
Dec 28 Javascript
jquery制作LED 时钟特效
Feb 01 Javascript
JavaScript正则表达式替换字符串中图片地址(img src)的方法
Jan 13 Javascript
laydate 显示结束时间不小于开始时间的实例
Aug 11 Javascript
vue中v-for循环给标签属性赋值的方法
Oct 18 Javascript
微信小程序实现提交input信息到后台的方法示例
Jan 19 Javascript
vue-router二级导航切换路由及高亮显示的实现方法
Jul 10 Javascript
vue $set 给数据赋值的实例
Nov 09 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版(2)
2006/10/09 PHP
thinkphp四种url访问方式详解
2014/11/28 PHP
PHP单链表的实现代码
2016/07/05 PHP
PHP网页安全认证的实例详解
2017/09/28 PHP
详解PHP字符串替换str_replace()函数四种用法
2017/10/13 PHP
详解关于php的xdebug配置(编辑器vscode)
2019/01/29 PHP
基于jquery的表格排序
2010/09/11 Javascript
jqPlot 基于jquery的画图插件
2011/04/26 Javascript
在jquery中处理带有命名空间的XML数据
2011/06/13 Javascript
当jQuery遭遇CoffeeScript的时候 使用分享
2011/09/17 Javascript
jQuery实现表头固定效果的实例代码
2013/05/24 Javascript
js与jQuery 获取父窗、子窗的iframe
2013/12/20 Javascript
jquery live()调用不存在的解决方法
2014/02/26 Javascript
AngularJS使用ngOption实现下拉列表的实例代码
2016/01/23 Javascript
js 毫秒转天时分秒的实例
2017/11/17 Javascript
vue2.0 elementUI制作面包屑导航栏
2018/02/22 Javascript
React Router V4使用指南(精讲)
2018/09/17 Javascript
layer实现登录弹框,登录成功后关闭弹框并调用父窗口的例子
2019/09/11 Javascript
layui layer select 选择被遮挡的解决方法
2019/09/21 Javascript
layui 数据表格+分页+搜索+checkbox+缓存选中项数据的方法
2019/09/21 Javascript
JS实现普通轮播图特效
2020/01/01 Javascript
纯JS开发baguetteBox.js响应式画廊插件
2020/06/28 Javascript
[51:28]EG vs Mineski 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/16 DOTA
python正常时间和unix时间戳相互转换的方法
2015/04/23 Python
Python selenium抓取微博内容的示例代码
2018/05/17 Python
pandas表连接 索引上的合并方法
2018/06/08 Python
Django admin.py 在修改/添加表单界面显示额外字段的方法
2019/08/22 Python
浅析pip安装第三方库及pycharm中导入第三方库的问题
2020/03/10 Python
Python openpyxl 插入折线图实例
2020/04/17 Python
python 逆向爬虫正确调用 JAR 加密逻辑
2021/01/12 Python
Burberry英国官网:英国标志性奢侈品牌
2017/03/29 全球购物
美国知名生活购物网站:Goop
2017/11/03 全球购物
污水处理保证书
2015/05/09 职场文书
十七岁的单车观后感
2015/06/12 职场文书
教师节晚会主持词
2015/06/30 职场文书
2016年重阳节慰问信
2015/12/01 职场文书