Vue+Openlayer批量設(shè)置閃爍點的實現(xiàn)代碼(基于postrender機制)
更新時間:2021年09月01日 17:26:21 作者:~疆
本篇文章給大家介紹基于postrender機制使用Vue+Openlayer批量設(shè)置閃爍點的實現(xiàn)代碼,代碼簡單易懂,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
效果圖:
實現(xiàn)代碼:
<template> <div id="map" style="width: 100vw; height: 100vh" /> </template> <script> import "ol/ol.css"; import TileLayer from "ol/layer/Tile"; import VectorLayer from "ol/layer/Vector"; import VectorSource from "ol/source/Vector"; import XYZ from "ol/source/XYZ"; import { Map, View, Feature } from "ol"; import { Style, Circle, Stroke } from "ol/style"; import { Point } from "ol/geom"; import { getVectorContext } from "ol/render"; // 邊界json數(shù)據(jù) export default { data() { return { map: {}, coordinates: [ { x: "106.918082", y: "31.441314" }, //重慶 { x: "86.36158200334317", y: "41.42448570787448" }, //新疆 { x: "89.71757707811526", y: "31.02619817424643" }, //西藏 { x: "116.31694544853109", y: "39.868508850821115" }, //北京 { x: "103.07940932026341", y: "30.438580338450862" }, //成都 ], speed: 0.3, }; }, mounted() { this.initMap(); this.addDynamicPoints(this.coordinates); }, methods: { /** * 初始化地圖 */ initMap() { this.map = new Map({ target: "map", layers: [ new TileLayer({ source: new XYZ({ url: "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}", }), }), ], view: new View({ projection: "EPSG:4326", center: [108.522097, 37.272848], zoom: 4.7, }), }); }, /** * 批量添加閃爍點 */ addDynamicPoints(coordinates) { // 設(shè)置圖層 let pointLayer = new VectorLayer({ source: new VectorSource() }); // 添加圖層 this.map.addLayer(pointLayer); // 循環(huán)添加feature let pointFeature = []; for (let i = 0; i < coordinates.length; i++) { // 創(chuàng)建feature,一個feature就是一個點坐標信息 const feature = new Feature({ geometry: new Point([coordinates[i].x, coordinates[i].y]), }); pointFeature.push(feature); } //把要素集合添加到圖層 pointLayer.getSource().addFeatures(pointFeature); // 關(guān)鍵的地方在此:監(jiān)聽postrender事件,在里面重新設(shè)置circle的樣式 let radius = 0; pointLayer.on("postrender", (e) => { if (radius >= 20) radius = 0; let opacity = (20 - radius) * (1 / 20); //不透明度 let pointStyle = new Style({ image: new Circle({ radius: radius, stroke: new Stroke({ color: "rgba(255,0,0" + opacity + ")", width: 3 - radius / 10, //設(shè)置寬度 }), }), }); // 獲取矢量要素上下文 let vectorContext = getVectorContext(e); vectorContext.setStyle(pointStyle); pointFeature.forEach((feature) => { vectorContext.drawGeometry(feature.getGeometry()); }); radius = radius + this.speed; //調(diào)整閃爍速度 //請求地圖渲染(在下一個動畫幀處) this.map.render(); }); }, }, }; </script>
到此這篇關(guān)于Vue+Openlayer批量設(shè)置閃爍點的實現(xiàn)代碼(基于postrender機制)的文章就介紹到這了,更多相關(guān)Vue Openlayer閃爍點內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue tab切換,解決echartst圖表寬度只有100px的問題
這篇文章主要介紹了vue tab切換,解決echartst圖表寬度只有100px的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue(element ui)使用websocket及心跳檢測方式
這篇文章主要介紹了vue(element ui)使用websocket及心跳檢測方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07Vue 通過自定義指令回顧v-內(nèi)置指令(小結(jié))
這篇文章主要介紹了Vue 通過自定義指令回顧v-內(nèi)置指令(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09Vue中nprogress頁面加載進度條的方法實現(xiàn)
這篇文章主要介紹了Vue中nprogress頁面加載進度條的方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11vue3利用v-model實現(xiàn)父子組件之間數(shù)據(jù)同步的代碼詳解
在Vue 3中,v-model這一指令也得到了升級,使得父子組件之間的數(shù)據(jù)同步變得更加容易和靈活,本文將探討一下Vue 3中如何利用v-model來實現(xiàn)父子組件之間的數(shù)據(jù)同步,需要的朋友可以參考下2024-03-03