当前位置: 首页 > news >正文

vue openlayers地图加载大量点位时优化

vue openlayers地图加载大量点位时优化

如果一次性加载上万个带标题的点位,会造成地图卡顿, 优化方法是只加载当前视口内的点位,且只显示屏幕中心的点位的标题, 每次拖动地图只加载视口内的点位

  • 工具类OlViewportPointUtil.js
importVectorLayerfrom'ol/layer/Vector';importVectorSourcefrom'ol/source/Vector';importPointfrom'ol/geom/Point';importFeaturefrom'ol/Feature';importStylefrom'ol/style/Style';importIconfrom'ol/style/Icon';importTextfrom'ol/style/Text';importFillfrom'ol/style/Fill';importStrokefrom'ol/style/Stroke';import{containsCoordinate,intersects}from'ol/extent';import{fromLonLat}from'ol/proj';/** * 显示可见范围内的点位 */exportdefaultclassOlViewportPointUtil{constructor(map,options={}){this.map=map;this.allPoints=[];this.vectorSource=null;this.vectorLayer=null;this.options={style:this.getDefaultStyle.bind(this),debounceTime:100,minZoom:3,...options,};this.initLayer();this.bindMapEvent();}initLayer(){this.vectorSource=newVectorSource();this.vectorLayer=newVectorLayer({source:this.vectorSource,zIndex:999,declutter:true,declutterMode:'label',style:this.options.style,});this.map.addLayer(this.vectorLayer);}getDefaultStyle(feature,resolution){// 最小缩放等级判断constcurrentZoom=this.map.getView().getZoom();if(currentZoom<this.options.minZoom){returnnull;}// 文字显示判断(中心区域才显示)constshowLabel=getIsShowFeatureText(this.map,feature);constpointData=feature.get('data');returnnewStyle({image:newIcon({src:require('../assets/点位图标.png'),scale:0.5,anchor:[0.5,1],}),text:showLabel?newText({text:pointData?.name||'',// 从 feature 里取标题font:'14px Microsoft YaHei',fill:newFill({color:'#ffffff'}),stroke:newStroke({color:'#1a5a96',width:2}),offsetY:-25,textAlign:'center',}):null,});}setDataSource(points=[]){if(!points||points.length===0)return;this.allPoints=points;this.refreshView();}refreshView(){this.vectorSource.clear();constextent=this.map.getView().calculateExtent(this.map.getSize());constfeatures=[];for(constpointofthis.allPoints){constjd=parseFloat(point.jd);constwd=parseFloat(point.wd);// const coord = fromLonLat([jd, wd]);constcoord=[jd,wd];if(containsCoordinate(extent,coord)){constfeature=newFeature({geometry:newPoint(coord),data:point,// 把数据存到 feature 里});features.push(feature);}}console.log('当前视口内点位数量:',features.length);this.vectorSource.addFeatures(features);}bindMapEvent(){lettimer=null;this.map.on('moveend',()=>{clearTimeout(timer);timer=setTimeout(()=>{this.refreshView();},this.options.debounceTime);});}clear(){this.vectorSource.clear();this.allPoints=[];}destroy(){this.clear();this.map.removeLayer(this.vectorLayer);}}/** * 只显示屏幕中心附近的名称 */functiongetIsShowFeatureText(map,feature){constview=map.getView();constmapSize=map.getSize();if(!mapSize)returnfalse;constviewExtent=view.calculateExtent(mapSize);const[minx,miny,maxx,maxy]=viewExtent;constw=maxx-minx;consth=maxy-miny;constcenterBuffer=0.3;constcenterExtent=[minx+w*centerBuffer,miny+h*centerBuffer,maxx-w*centerBuffer,maxy-h*centerBuffer];constgeom=feature.getGeometry();returngeom&&intersects(geom.getExtent(),centerExtent);}
  • 使用
importOlViewportPointUtilfrom'./olViewportPointUtil.js';constlist=[{name:'点位1',jd:'经度',wd:'纬度'},......]this.pointUtil=newOlViewportPointUtil(this.map);this.pointUtil.setDataSource(list);
http://www.jsqmd.com/news/659318/

相关文章:

  • C语言这么牛,它自身又是用什么语言写的?真相很硬核
  • 手把手教你用AI手势识别:上传图片秒出彩虹骨骼图,无需编程
  • 别再自己画封装了!用这三个免费网站,5分钟搞定AD原理图和PCB库
  • Ostrakon-VL终端快速上手:扫码登录+微信小程序联动方案
  • GLM-OCR模型Java开发集成指南:SpringBoot微服务中的文档处理实战
  • Clawdbot代理网关快速上手:5分钟部署Qwen3:32B本地大模型
  • 用 Gemini 打造 10 分钟完美行程的五个“降维打击”技巧
  • 8、新的开始:返璞归真,使用最简单的ElementPlus来实现本项目
  • 【好靶场】你知道unionId吗
  • GEO 1.0 到 2.0:为什么 90% 的品牌优化是表面功夫
  • Jetson Orin Nano开发者必看:PyTorch环境一键配置指南(附常见错误排查)
  • AI超清画质增强自动化流水线:CI/CD集成思路
  • 华为eNSP静态路由与动态路由综合实验报告
  • Qwen3-14B私有部署成本分析:RTX 4090D云主机月度费用测算
  • 供应商评估模型:从课程设计、讲师背景、案例库到售后支持的全方位对比
  • 别再死记硬背APB时序了!用状态机手把手教你写一个可复用的APB Master模块(Verilog代码详解)
  • Qwen1.5-1.8B GPTQ与Dify集成:快速构建无代码AI智能体应用
  • 2026 很多卖家做Temu卡住,不是能力问题,而是方式错了
  • cubeIDE创建不了,是版本的问题,然后你要下载包,不能没有STM32的固件包
  • 雪女-斗罗大陆-造相Z-Turbo数据处理:使用MATLAB进行生成结果的批量分析与可视化
  • 5分钟体验Qwen3语义搜索:GPU加速,结果可视化,操作极简
  • 创意无限:用ComfyUI Qwen人脸生成,为社交媒体打造独一无二的虚拟形象
  • MusePublic Art Studio部署指南:Windows11环境一键安装教程
  • STM32调试实战:Keil MDK + J-Link下局部变量消失的5种排查姿势
  • 从理论到实测:全国电赛D题电路特性测试仪之输出阻抗、增益与上限频率实战解析
  • 告别移植烦恼:手把手教你用NRF52832的ESB库直连NRF24L01模块(附完整代码)
  • LeetCode442 数组中重复的数据|原地哈希空间优化算法C++深度题解
  • Qwen1.5-1.8B-GPTQ-Int4部署教程:WSL2环境下Windows本地轻量AI开发环境搭建
  • 113页精品PPT | 智慧校园智能化系统方案
  • 新手安装HBase