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

OpenLayers 综合案例-区域掩膜

看过的知识不等于学会。唯有用心总结、系统记录,并通过温故知新反复实践,才能真正掌握一二
作为一名摸爬滚打三年的前端开发,开源社区给了我饭碗,我也将所学的知识体系回馈给大家,助你少走弯路!
OpenLayers、Leaflet 快速入门 ,每周保持更新 2 个案例
Cesium 快速入门,每周保持更新 4 个案例

OpenLayers 综合案例-区域掩膜

Vue 3 + OpenLayers 实现的 WebGIS 应用提供了完整的区域掩膜功能

实现思路

  1. 主要就是考验 canvas 的使用,核心代码参考openlayers 中区域掩膜的实现
  2. 在地图容器中添加一个 canvas,设置其在 map 之上;
  3. 监听 map 的 postrender 事件,每次事件触发重新绘制掩膜;
  4. 通过 map.getPixelFromCoordinate 实现地理坐标到屏幕坐标的转换;
  5. 通过 globalCompositeOperation = 'source-out’设置反向裁剪;

在这里插入图片描述

MP4效果动画链接地址&JSON数据获取地址

技术栈

该环境下代码即拿即用

Vue 3.5.13+
OpenLayers 10.5.0+
Vite 6.3.5+
<template><div ref="mapContainer" id="map"></div>
</template><script setup>
import { ref, onMounted } from "vue";
import Map from "ol/Map.js";
import XYZ from "ol/source/XYZ.js";
import TileLayer from "ol/layer/Tile.js";
import View from "ol/View.js";
import "ol/ol.css";
import modalData from "./320000_bj.json";const mapContainer = ref(null);
let map = null;
let canvas = null;
let ctx = null;const view = new View({center: [118.7784, 32.0647], // 南京市中心经纬度zoom: 7,projection: "EPSG:4326",
});onMounted(async () => {map = new Map({target: mapContainer.value,layers: [new TileLayer({source: new XYZ({url: "https://webrd04.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}",}),}),],view,});// 创建canvasconst { offsetWidth, offsetHeight } = map.getViewport();canvas = document.createElement("canvas");canvas.width = offsetWidth;canvas.height = offsetHeight;canvas.style.position = "absolute";canvas.style.top = "0px";canvas.style.left = "0px";canvas.style.zIndex = "1";ctx = canvas.getContext("2d");map.getViewport().appendChild(canvas);// 注册map事件map.on("postrender", () => {addMask();});
});// 添加区域掩膜
const addMask = (params) => {const { fillStyle, strokeStyle, lineWidth } = {fillStyle: "rgba(255,255,255,0.8)",strokeStyle: "#f00",lineWidth: 3,...params,};ctx.clearRect(0, 0, canvas.width, canvas.height);// 获取整个江苏省的所有多边形const jiangsuPolygons = modalData.features[0].geometry.coordinates;// 1. 绘制整个画布为半透明白色ctx.fillStyle = fillStyle;ctx.fillRect(0, 0, canvas.width, canvas.height);// 2. 使用组合模式清除多边形区域ctx.globalCompositeOperation = "destination-out";ctx.fillStyle = "rgba(0,0,0,1)"; // 使用任意颜色,alpha=1确保完全清除// 绘制所有多边形(包括主区域和岛屿)jiangsuPolygons.forEach((polygon) => {const ring = polygon[0]; // 获取多边形外环const coords = ring.map((coord) => map.getPixelFromCoordinate(coord));ctx.beginPath();coords.forEach((coord, index) => {index === 0? ctx.moveTo(coord[0], coord[1]): ctx.lineTo(coord[0], coord[1]);});ctx.closePath();ctx.fill();});// 3. 恢复组合模式并绘制边界ctx.globalCompositeOperation = "source-over";ctx.strokeStyle = strokeStyle;ctx.lineWidth = lineWidth;// 绘制所有多边形的边界jiangsuPolygons.forEach((polygon) => {const ring = polygon[0];const coords = ring.map((coord) => map.getPixelFromCoordinate(coord));ctx.beginPath();coords.forEach((coord, index) => {index === 0? ctx.moveTo(coord[0], coord[1]): ctx.lineTo(coord[0], coord[1]);});ctx.closePath();ctx.stroke();});
};
</script><style scoped>
#map {position: absolute;top: 0;bottom: 0;width: 100vw;height: 100vh;
}
</style>
http://www.dtcms.com/a/302981.html

相关文章:

  • JavaWeb 新手学习路线:从零到全栈开发,系统掌握企业级 Web 开发技能
  • 【Java Web实战】从零到一打造企业级网上购书网站系统 | 完整开发实录(三)
  • 7.DRF 过滤、排序、分页
  • 微信小程序——早餐小程序
  • 22 BTLO 蓝队靶场 Countdown 解题记录
  • 博士申请 | 荷兰阿姆斯特丹大学 招收计算机视觉(CV)方向 全奖博士生
  • 代码的“防伪水印“:签名证书的隐形安全标记
  • 【python高阶】-1- python工程和线程并发
  • 电路防倒灌设计 --- 双pmos防倒灌
  • 学习dify:一个开源的 LLM 应用开发平台
  • AUTOSAR标准下CAN网络休眠异常诊断机制的创新研究
  • SD卡简介与驱动开发
  • Linux环境-设置Anaconda环境变量 Anaconda路径
  • 力扣1315. 祖父节点值为偶数的节点和
  • TDSQL 技术详解
  • Codeforces Round 1039 (Div. 2)(A-E1)
  • 【前端】React 与 Vue:前端两大框架的全方位对比解析
  • CMakelists.txt 实现多级目录编译
  • redis未授权getshell四种方式
  • 【ELasticsearch】搭建有负载均衡 ELB 的 ES 集群
  • TongESBv7报错:DatabaseConnectionException: no connection available(by lqw)
  • 正则表达式 速查速记
  • 多数据库学习之VastbaseG100海量数据库入门实践
  • Spring AI 1.0 提供简单的 AI 系统和服务
  • opencv 模块裁剪 按需安装指定模块
  • 《零基础入门AI: 从轮廓查找到形态学变换(OpenCV图像预处理)》
  • 【数据架构09】人工智能及数据智能架构篇
  • Charles中文版深度解析,轻松调试API与优化网络请求
  • 产品需求如何系统化管理
  • 简明量子态密度矩阵理论知识点总结