vue使用openlayers實(shí)現(xiàn)移動(dòng)點(diǎn)動(dòng)畫(huà)
本文實(shí)例為大家分享了vue使用openlayers實(shí)現(xiàn)移動(dòng)點(diǎn)動(dòng)畫(huà)的具體代碼,供大家參考,具體內(nèi)容如下
做項(xiàng)目時(shí),本來(lái)打算仿照官網(wǎng)的Example中動(dòng)畫(huà)制作,引入vue中后,發(fā)現(xiàn)它引用的庫(kù)函數(shù)一直報(bào)錯(cuò),最后我去vue中安裝的依賴庫(kù)中去查找這個(gè)函數(shù),果然沒(méi)有。也就是說(shuō)官方例子使用的庫(kù)和我安裝的OL庫(kù)存在一定差異。
后來(lái)我還是用笨方法去解決了,最終效果如下:

總體思路是將移動(dòng)目標(biāo)實(shí)例一個(gè)Overlay對(duì)象,然后將如圖5個(gè)經(jīng)緯度點(diǎn)沒(méi)兩點(diǎn)之間分割成多個(gè)(200個(gè)),之后通過(guò)定時(shí)器不斷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/船載應(yīng)急通信系統(tǒng).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, // 路徑點(diǎn)索引
marker: null,//移動(dòng)點(diǎn)
splitNumber: 200, // 每?jī)蓚€(gè)經(jīng)緯度之間的分割點(diǎn)
setIntervalTime: 30, // 移動(dòng)點(diǎn)間隔時(shí)間
myplanImg: myplanImg, // 移動(dòng)點(diǎn)的圖片
helpTooltipElement: null, // 平臺(tái)信息div
helpTooltip: null // 平臺(tái)信息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({// 兩端點(diǎn)Feature
source: sourceFeatures
})
var lineString = new geom.LineString([])
var layerRoute = new layer.Vector({// 兩點(diǎn)之間的連線
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 = [// 開(kāi)始結(jié)束點(diǎn)樣式
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>船載應(yīng)急通信系統(tǒng)' + '\<br\>' +
'<B>子系統(tǒng):</B>平臺(tái)A,平臺(tái)B' + '\<br\>' +
'<B>經(jīng)緯度:</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++) { // 每?jī)蓚€(gè)點(diǎn)之間分割出splitNumber個(gè)點(diǎn)
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中手機(jī)號(hào),郵箱正則驗(yàn)證以及60s發(fā)送驗(yàn)證碼的實(shí)例
下面小編就為大家分享一篇vue中手機(jī)號(hào),郵箱正則驗(yàn)證以及60s發(fā)送驗(yàn)證碼的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
去掉vue 中的代碼規(guī)范檢測(cè)兩種方法(Eslint驗(yàn)證)
我們?cè)谑褂胿ue 腳手架時(shí),為了規(guī)范團(tuán)隊(duì)的代碼格式,會(huì)有一個(gè)代碼規(guī)范檢測(cè),如果不符合規(guī)范就會(huì)報(bào)錯(cuò),有時(shí)候我們不想按照他的規(guī)范去寫。這時(shí)我們需要關(guān)閉,這里腳本之家小編給大家?guī)?lái)了去掉vue 中的代碼規(guī)范檢測(cè)兩種方法(Eslint驗(yàn)證),一起看看吧2018-03-03
Vue開(kāi)發(fā)過(guò)程中遇到的疑惑知識(shí)點(diǎn)總結(jié)
vue是法語(yǔ)中視圖的意思,Vue.js是一個(gè)輕巧、高性能、可組件化的MVVM庫(kù),同時(shí)擁有非常容易上手的API。下面這篇文章主要給大家總結(jié)了Vue在開(kāi)發(fā)過(guò)程中遇到的疑惑知識(shí)點(diǎn),有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-01-01
vue中用動(dòng)態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果
本篇文章主要介紹了vue中用動(dòng)態(tài)組件實(shí)現(xiàn)選項(xiàng)卡切換效果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Vue動(dòng)態(tài)修改網(wǎng)頁(yè)標(biāo)題的方法及遇到問(wèn)題
Vue下有很多的方式去修改網(wǎng)頁(yè)標(biāo)題,這里總結(jié)下解決此問(wèn)題的幾種方案:,需要的朋友可以參考下2019-06-06
Vue 實(shí)現(xiàn)從文件中獲取文本信息的方法詳解
這篇文章主要介紹了Vue 實(shí)現(xiàn)從文件中獲取文本信息的方法,結(jié)合實(shí)例形式詳細(xì)分析了vue.js基于export導(dǎo)出的文件信息讀取相關(guān)操作技巧,需要的朋友可以參考下2019-10-10
22個(gè)Vue優(yōu)化技巧(項(xiàng)目實(shí)用)
演示代碼使用 Vue3 + ts + Vite 編寫,但是也會(huì)列出適用于 Vue2 的優(yōu)化技巧,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
element ui loading加載開(kāi)啟與關(guān)閉方式
這篇文章主要介紹了element ui loading加載開(kāi)啟與關(guān)閉方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

