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

leaflet 矢量 经纬网格

1. 绘制需求

1)每层级网格像素大小不变,即每层级,网格经纬度距离加密一次;

2)页面顶部和右侧标注经度、纬度值;

3)经纬度值精度随层级变化;

4)尽量避免显示区域外的多余绘制,同时保证拖动时不会出现没有网格的情况;

2. 代码实现

1)确定网格线间隔,经线范围:-180~180;纬线:-90~90;

间隔值应当为180、90的公约数,这样能保证过0°经线;比如:

1级:-180、-90、0、90、180

2级:-180、-135、-90、-45、0、45、90、135、180

......

let zoom = map.getZoom(); // 经纬度间隔 let d = 90 / Math.pow(2, zoom - 1);

2) 只绘制当前视野内的线

3)在边界上添加标注

let zoom = map.getZoom(); let bounds = map.getBounds(); let north = bounds.getNorth(); let east = bounds.getEast(); // 经纬度间隔 let d = 90 / Math.pow(2, zoom - 1); // 经线网格 for (let index = -180; index <= 360; index += d) { // 判断当前视野内 if (bounds.contains([north, index])) { // 绘制经线 let lonLine = L.polyline( [ [-90, index], [90, index], ], { weight: 1, color: "blue" } ); lonLatGridLineLayer.addLayer(lonLine); // 标注 let text = index.toFixed(1) + "°"; // 动态计算小数位数 if (zoom > 10) { text = index.toFixed((zoom - 8) / 2) + "°"; } let divIcon = L.divIcon({ html: `<div style="white-space: nowrap;color:red;">${text}</div>`, iconAnchor: [0, -5], }); let textMarker = L.marker([north, index], { icon: divIcon }); lonLatGridLineLayer.addLayer(textMarker); } }

4)缩放和拖动事件,重绘网格

map.on("zoomend move", () => { lonLatGridLineLayer.clearLayers(); addLonLatLine(); });

3.全部代码

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" /> <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script> <title>leaflet-经纬网格</title> <style> html, body { width: 100%; height: 100%; padding: 0; margin: 0; } .leaflet-div-icon { background: none; border: none; } </style> </head> <body> <div id="map" style="height: 100%; width: 100%"></div> <script> let map = L.map("map", { renderer: L.canvas({ padding: 0.5 }) }).setView( [25.127879288597576, 118.37905883789064], 4 ); // 添加背景图层 L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors', }).addTo(map); // 创建图层 let lonLatGridLineLayer = L.featureGroup().addTo(map); // 经纬网格生成方法 let addLonLatLine = () => { let zoom = map.getZoom(); let bounds = map.getBounds(); let north = bounds.getNorth(); let east = bounds.getEast(); // 经纬度间隔 let d = 90 / Math.pow(2, zoom - 1); // 经线网格 for (let index = -180; index <= 360; index += d) { // 判断当前视野内 if (bounds.contains([north, index])) { // 绘制经线 let lonLine = L.polyline( [ [-90, index], [90, index], ], { weight: 1, color: "blue" } ); lonLatGridLineLayer.addLayer(lonLine); // 标注 let text = index.toFixed(1) + "°"; // 动态计算小数位数 if (zoom > 10) { text = index.toFixed((zoom - 8) / 2) + "°"; } let divIcon = L.divIcon({ html: `<div style="white-space: nowrap;color:red;">${text}</div>`, iconAnchor: [0, -5], }); let textMarker = L.marker([north, index], { icon: divIcon }); lonLatGridLineLayer.addLayer(textMarker); } } if(d>90)d=90; // 纬线网格 for (let index = -90; index <= 90; index += d) { if (bounds.contains([index, east])) { let lonLine = L.polyline( [ [index, -180], [index, 360], ], { weight: 1, color: "blue" } ); lonLatGridLineLayer.addLayer(lonLine); // 标注 let text = index.toFixed(1) + "°"; if (zoom > 10) { text = index.toFixed((zoom - 8) / 2) + "°"; } let divIcon = L.divIcon({ html: `<div style="white-space: nowrap;color:red;">${text}</div>`, iconAnchor: [(text.length + 1) * 6, 0], }); let textMarker = L.marker([index, east], { icon: divIcon }); lonLatGridLineLayer.addLayer(textMarker); } } }; addLonLatLine(); map.on("zoomend move", () => { lonLatGridLineLayer.clearLayers(); addLonLatLine(); }); </script> </body> </html>
http://www.jsqmd.com/news/1194719/

相关文章:

  • 2026大理厨卫漏水维修 5家靠谱修缮品牌推荐,精准检测漏点、定位隐蔽漏水位置 - 宅安选房屋修缮
  • 深入解析C6748 DSP内存映射、启动模式与中断系统设计
  • 如何快速将Intel Intrinsic应用迁移到鲲鹏平台:AvxToNeon实战教程
  • 告别加班写报!ChatGPT周报月报自动化方案(含自动摘要+数据可视化+老板偏好学习功能),上线首周下载破12,800份
  • PHP Period库实战:5个真实场景的时间范围处理示例
  • Jido重试策略:失败操作的智能重试机制
  • 韩国大学与天主教大学联手打造的视觉标记压缩新方案
  • 2026北京继承纠纷律师费怎么算?先搞清六个影响因素 - 科技焦点
  • 企业出海前先回答三个问题,广汽和长安用十年走通了一条路
  • 宠物洗护宠物店如何提升老客复购?餐宝盈小程序GEO获客逻辑,全程零代码,SAAS+GEO模式
  • 你的PC小说阅读体验还停留在浏览器时代吗?Uncle小说阅读器重新定义桌面阅读
  • [特殊字符] 企业级IM即时通讯系统|安全、高效、灵活的私有化部署解决方案
  • 如何快速上手BiSheng JDK-build:10分钟完成JDK构建的终极教程
  • 如何5分钟掌握unrpa:终极RPA文件解包工具完整指南
  • 分布式通信范式革命:ET框架的量子纠缠式消息系统架构解密
  • 乔伊未来学院等机构找到了让AI“想清楚再下笔“的秘诀
  • EasyOCR终极指南:5个技巧实现多语言印刷体与手写体高效识别
  • 8~52V宽压输入1A步进电机驱动器设计全解析
  • 终极指南:用Citra模拟器在PC上完美运行3DS游戏
  • WSL+OpenCode:Windows搭建AI编程环境的完整指南
  • Windows Home Server 2011安装配置指南:兼容性挑战与现代硬件部署实践
  • 探秘江苏省工程造价公司:专业服务背后隐藏着怎样的行业奥秘? - 资讯速览
  • Python深入浅出:从入门到工程实践20
  • 【死磕JVM】用Arthas揪出内存泄漏元凶:从监控到定位的实战指南
  • 宝玑官网售后服务,腕表维修与保养服务权威公示(2026年7月最新) - 亨得利钟表维修中心
  • 查询|2026 上海劳力士官方售后地址及维修预约电话(2026年7月最新版) - 劳力士中国维修中心
  • Uncle小说阅读器:重新定义PC端数字阅读体验的三大技术突破
  • 如何快速搭建Java电商系统:基于Spring Boot和Vue的完整解决方案
  • PalEdit:3大场景解锁你的PalWorld存档编辑超能力
  • 构建企业级高分辨率说话人脸生成架构:HDTF深度技术实现指南