Vue+Openlayer批量设置闪烁点的实现代码(基于postrender机制)
程序员文章站
2022-07-03 21:37:36
效果图:实现代码:
效果图:
实现代码:
<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数据 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) { // 设置图层 let pointlayer = new vectorlayer({ source: new vectorsource() }); // 添加图层 this.map.addlayer(pointlayer); // 循环添加feature let pointfeature = []; for (let i = 0; i < coordinates.length; i++) { // 创建feature,一个feature就是一个点坐标信息 const feature = new feature({ geometry: new point([coordinates[i].x, coordinates[i].y]), }); pointfeature.push(feature); } //把要素集合添加到图层 pointlayer.getsource().addfeatures(pointfeature); // 关键的地方在此:监听postrender事件,在里面重新设置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, //设置宽度 }), }), }); // 获取矢量要素上下文 let vectorcontext = getvectorcontext(e); vectorcontext.setstyle(pointstyle); pointfeature.foreach((feature) => { vectorcontext.drawgeometry(feature.getgeometry()); }); radius = radius + this.speed; //调整闪烁速度 //请求地图渲染(在下一个动画帧处) this.map.render(); }); }, }, }; </script>
参考:https://xiehao.blog.csdn.net/article/details/119955823
到此这篇关于vue+openlayer批量设置闪烁点的实现代码(基于postrender机制)的文章就介绍到这了,更多相关vue openlayer闪烁点内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!