三十五、openlayers官网示例Dynamic Data——在地图上加载动态数据形成动画效果

本文主要是介绍三十五、openlayers官网示例Dynamic Data——在地图上加载动态数据形成动画效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官网demo地址:

 Dynamic Data

 初始化地图

 const tileLayer = new TileLayer({source: new OSM(),});const map = new Map({layers: [tileLayer],target: "map",view: new View({center: [0, 0],zoom: 2,}),});

    创建了三个样式

const imageStyle = new Style({image: new CircleStyle({radius: 5,fill: new Fill({ color: "yellow" }),stroke: new Stroke({ color: "red", width: 1 }),}),});const headInnerImageStyle = new Style({image: new CircleStyle({radius: 2,fill: new Fill({ color: "blue" }),}),});const headOuterImageStyle = new Style({image: new CircleStyle({radius: 5,fill: new Fill({ color: "black" }),}),});

绘制动画效果主要还是利用了postrender事件,其原理解析可以参考这篇

十八、openlayers官网示例Custom Animation解析——地图上添加自定义动画、圆圈涟漪效果_unbykey(listenerkey);-CSDN博客

// 点的数量,用于定义图形的分辨率const n = 200;// 角速度常量,用于计算当前时间的角度const omegaTheta = 30000;// 大圆的半径const R = 7e6;// 小圆的半径const r = 2e6;// 距离小圆中心的点的距离const p = 2e6;tileLayer.on("postrender", function (event) {//获取渲染上下文和帧状态//用于在地图上绘制矢量图形const vectorContext = getVectorContext(event);//包含当前帧的状态,包括时间信息const frameState = event.frameState;//计算当前角度 theta 根据当前时间和 omegaTheta 计算当前角度 theta,用于动画效果const theta = (2 * Math.PI * frameState.time) / omegaTheta;//生成外旋轮线的坐标数组const coordinates = [];let i;for (i = 0; i < n; ++i) {const t = theta + (2 * Math.PI * i) / n;const x = (R + r) * Math.cos(t) + p * Math.cos(((R + r) * t) / r);const y = (R + r) * Math.sin(t) + p * Math.sin(((R + r) * t) / r);coordinates.push([x, y]);}vectorContext.setStyle(imageStyle);//设置样式 imageStyle 并绘制多点几何(MultiPoint)vectorContext.drawGeometry(new MultiPoint(coordinates));const headPoint = new Point(coordinates[coordinates.length - 1]);//将头部的圆标注出来设置成不同样式vectorContext.setStyle(headOuterImageStyle);vectorContext.drawGeometry(headPoint);vectorContext.setStyle(headInnerImageStyle);vectorContext.drawGeometry(headPoint);//强制重新渲染地图map.render();});

完整代码:

<template><div class="box"><h1>Dynamic Data动态数据</h1><div id="map"></div></div>
</template><script>
import Map from "ol/Map.js";
import OSM from "ol/source/OSM.js";
import TileLayer from "ol/layer/Tile.js";
import View from "ol/View.js";
import { Circle as CircleStyle, Fill, Stroke, Style } from "ol/style.js";
import { MultiPoint, Point } from "ol/geom.js";
import { getVectorContext } from "ol/render.js";
export default {name: "",components: {},data() {return {map: null,};},computed: {},created() {},mounted() {const tileLayer = new TileLayer({source: new OSM(),});const map = new Map({layers: [tileLayer],target: "map",view: new View({center: [0, 0],zoom: 2,}),});const imageStyle = new Style({image: new CircleStyle({radius: 5,fill: new Fill({ color: "yellow" }),stroke: new Stroke({ color: "red", width: 1 }),}),});const headInnerImageStyle = new Style({image: new CircleStyle({radius: 2,fill: new Fill({ color: "blue" }),}),});const headOuterImageStyle = new Style({image: new CircleStyle({radius: 5,fill: new Fill({ color: "black" }),}),});// 点的数量,用于定义图形的分辨率const n = 200;// 角速度常量,用于计算当前时间的角度const omegaTheta = 30000;// 大圆的半径const R = 7e6;// 小圆的半径const r = 2e6;// 距离小圆中心的点的距离const p = 2e6;tileLayer.on("postrender", function (event) {//获取渲染上下文和帧状态//用于在地图上绘制矢量图形const vectorContext = getVectorContext(event);//包含当前帧的状态,包括时间信息const frameState = event.frameState;//计算当前角度 theta 根据当前时间和 omegaTheta 计算当前角度 theta,用于动画效果const theta = (2 * Math.PI * frameState.time) / omegaTheta;//生成外旋轮线的坐标数组const coordinates = [];let i;for (i = 0; i < n; ++i) {const t = theta + (2 * Math.PI * i) / n;const x = (R + r) * Math.cos(t) + p * Math.cos(((R + r) * t) / r);const y = (R + r) * Math.sin(t) + p * Math.sin(((R + r) * t) / r);coordinates.push([x, y]);}vectorContext.setStyle(imageStyle);//设置样式 imageStyle 并绘制多点几何(MultiPoint)vectorContext.drawGeometry(new MultiPoint(coordinates));const headPoint = new Point(coordinates[coordinates.length - 1]);//将头部的圆标注出来设置成不同样式vectorContext.setStyle(headOuterImageStyle);vectorContext.drawGeometry(headPoint);vectorContext.setStyle(headInnerImageStyle);vectorContext.drawGeometry(headPoint);//强制重新渲染地图map.render();});map.render();},methods: {},
};
</script><style lang="scss" scoped>
#map {width: 100%;height: 500px;
}
.box {height: 100%;
}
</style>

这篇关于三十五、openlayers官网示例Dynamic Data——在地图上加载动态数据形成动画效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1028921

相关文章

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

MyBatis延迟加载与多级缓存全解析

《MyBatis延迟加载与多级缓存全解析》文章介绍MyBatis的延迟加载与多级缓存机制,延迟加载按需加载关联数据提升性能,一级缓存会话级默认开启,二级缓存工厂级支持跨会话共享,增删改操作会清空对应缓... 目录MyBATis延迟加载策略一对多示例一对多示例MyBatis框架的缓存一级缓存二级缓存MyBat

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter

Java高效实现PowerPoint转PDF的示例详解

《Java高效实现PowerPoint转PDF的示例详解》在日常开发或办公场景中,经常需要将PowerPoint演示文稿(PPT/PPTX)转换为PDF,本文将介绍从基础转换到高级设置的多种用法,大家... 目录为什么要将 PowerPoint 转换为 PDF安装 Spire.Presentation fo

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

python中的高阶函数示例详解

《python中的高阶函数示例详解》在Python中,高阶函数是指接受函数作为参数或返回函数作为结果的函数,下面:本文主要介绍python中高阶函数的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录1.定义2.map函数3.filter函数4.reduce函数5.sorted函数6.自定义高阶函数

Vue实现路由守卫的示例代码

《Vue实现路由守卫的示例代码》Vue路由守卫是控制页面导航的钩子函数,主要用于鉴权、数据预加载等场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一、概念二、类型三、实战一、概念路由守卫(Navigation Guards)本质上就是 在路

JAVA实现Token自动续期机制的示例代码

《JAVA实现Token自动续期机制的示例代码》本文主要介绍了JAVA实现Token自动续期机制的示例代码,通过动态调整会话生命周期平衡安全性与用户体验,解决固定有效期Token带来的风险与不便,感兴... 目录1. 固定有效期Token的内在局限性2. 自动续期机制:兼顾安全与体验的解决方案3. 总结PS

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

Python屏幕抓取和录制的详细代码示例

《Python屏幕抓取和录制的详细代码示例》随着现代计算机性能的提高和网络速度的加快,越来越多的用户需要对他们的屏幕进行录制,:本文主要介绍Python屏幕抓取和录制的相关资料,需要的朋友可以参考... 目录一、常用 python 屏幕抓取库二、pyautogui 截屏示例三、mss 高性能截图四、Pill