当前位置: 首页 > 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.xdnf.cn/news/1205029.html

相关文章:

  • [机缘参悟-237]:AI人工神经网络与人类的神经网络工作原理的相似性
  • SpringBoot数学实例:高等数学实战
  • 7.项目起步(1)
  • Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型实现面部口罩的检测识别(C#代码,UI界面版)
  • 数据结构(动态数组)
  • HTML应用指南:利用GET请求获取全国小米之家门店位置信息
  • 第4章唯一ID生成器——4.2 单调递增的唯一ID
  • 【Zustand】从复杂到简洁:Zustand 状态管理简化实战指南
  • 绿算技术携手昇腾发布高性能全闪硬盘缓存设备,推动AI大模型降本增效
  • Laravel 分页方案整理
  • 安宝特新闻丨Vuzix与Wyr.Ai合作推出基于M400眼镜的全新一代质检平台
  • springboot校园外卖配送系统
  • 【设计模式】状态模式 (状态对象(Objects for States))
  • Linux应用程序架构与软件包管理
  • Redis实战(3)-- 高级数据结构zset
  • MySQL5.7主从延迟高排查优化思路
  • Qt:盒子模型的理解
  • 电流变送器电路的分析与计算
  • TCPIP之常用协议
  • LeetCode--50.Pow(x,n)
  • RCLAMP2574N.TCT Semtech:超低钳位TVS二极管 0.5pF超低电容+±30kV超强防护
  • FastGPT本地构建工作流高级编排(最新4.11.0)
  • 【云馨AI-大模型】2025世界人工智能大会引爆全球AI热潮,技术突破与政策布局引领产业新未来
  • 4、如何生成分布式ID?
  • C++中既重要又困难的部分—类和对象
  • 【历史人物】【韩愈】简历与生平
  • sqlite3学习---基础知识、增删改查和排序和限制、打开执行关闭函数
  • 归雁思维:解锁自然规律与人类智慧的桥梁
  • LLM学习笔记5——InstructGPT
  • Kotlin的datetime库