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

二十八、openlayers官网示例Data Tiles解析——自定义绘制DataTile源数据

官网demo地址:

 

https://openlayers.org/en/latest/examples/data-tiles.html

这篇示例讲解的是自定义加载DataTile源格式的数据。

先来看一下什么是DataTile,这个源是一个数组,与我们之前XYZ切片源有所不同。DataTile主要适用于需要动态生成、处理或渲染瓦片数据的复杂场景。

先新建一个canvas,设置一下画布参数。

   //256x256 像素是瓦片图层的常见尺寸const size = 256;const canvas = document.createElement("canvas");canvas.width = size;canvas.height = size;const context = canvas.getContext("2d");//设置描边颜色为白色。context.strokeStyle = "white";//设置文本对齐方式为居中context.textAlign = "center";//设置字体为 24 像素的无衬线字体。context.font = "24px sans-serif";//用于控制文本行高const lineHeight = 30;

 loader 是一个自定义数据加载函数,用于在需要时生成或获取瓦片数据。它的设计目的是为了处理动态生成的或特定格式的数据,比如在运行时计算或从非标准源获取的数据。

new TileLayer({source: new DataTile({loader: function (z, x, y) {const half = size / 2;//清除画布内容context.clearRect(0, 0, size, size);context.fillStyle = "rgba(100, 100, 100, 0.5)";//填充整个画布context.fillRect(0, 0, size, size);context.fillStyle = "red";//绘制文字context.fillText(`z: ${z}`, half, half - lineHeight);context.fillText(`x: ${x}`, half, half);context.fillText(`y: ${y}`, half, half + lineHeight);context.strokeRect(0, 0, size, size);//获取画布内容的像素数据const data = context.getImageData(0, 0, size, size).data;// 转换为Uint8Array以提高浏览器兼容性return new Uint8Array(data.buffer);},//禁用不透明度过渡,以避免在tile加载期间重叠标签transition: 0,}),}),

事实上,很多源都提供loader参数方便我们把获取的数据或地图路径经过二次处理之后再加载到地图上。而具体返回什么样的数据格式取决于源本身所接受的数据格式。

完整代码:

<template><div class="box"><h1>Data Tiles自定义绘制DataTile源数据</h1><div id="map"></div></div>
</template><script>
import DataTile from "ol/source/DataTile.js";
import Map from "ol/Map.js";
import TileLayer from "ol/layer/WebGLTile.js";
import View from "ol/View.js";
export default {name: "",components: {},data() {return {map: null,};},computed: {},created() {},mounted() {//256x256 像素是瓦片图层的常见尺寸const size = 256;const canvas = document.createElement("canvas");canvas.width = size;canvas.height = size;const context = canvas.getContext("2d");//设置描边颜色为白色。context.strokeStyle = "white";//设置文本对齐方式为居中context.textAlign = "center";//设置字体为 24 像素的无衬线字体。context.font = "24px sans-serif";//用于控制文本行高const lineHeight = 30;const map = new Map({target: "map",layers: [new TileLayer({source: new DataTile({loader: function (z, x, y) {const half = size / 2;//清除画布内容context.clearRect(0, 0, size, size);context.fillStyle = "rgba(100, 100, 100, 0.5)";//填充整个画布context.fillRect(0, 0, size, size);context.fillStyle = "red";//绘制文字context.fillText(`z: ${z}`, half, half - lineHeight);context.fillText(`x: ${x}`, half, half);context.fillText(`y: ${y}`, half, half + lineHeight);context.strokeRect(0, 0, size, size);//获取画布内容的像素数据const data = context.getImageData(0, 0, size, size).data;// 转换为Uint8Array以提高浏览器兼容性return new Uint8Array(data.buffer);},//禁用不透明度过渡,以避免在tile加载期间重叠标签transition: 0,}),}),],view: new View({center: [0, 0],zoom: 0,}),});},methods: {},
};
</script><style lang="scss" scoped>
#map {width: 100%;height: 500px;
}
.box {height: 100%;
}#info {width: 100%;height: 24rem;overflow: scroll;display: flex;align-items: baseline;border: 1px solid black;justify-content: flex-start;
}
</style>

http://www.lryc.cn/news/355071.html

相关文章:

  • 分布式事务解决方案(最终一致性【TCC解决方案】)
  • App Inventor 2 Encrypt.Security 安全性扩展:MD5哈希,SHA/AES/RSA/BASE64
  • 深入了解Linux中的环境变量
  • 雷军-2022.8小米创业思考-8-和用户交朋友,非粉丝经济;性价比是最大的诚意;新媒体,直播离用户更近;用真诚打动朋友,脸皮厚点!
  • 【Vue2.x】props技术详解
  • C语言例题46、根据公式π/4=1-1/3+1/5-1/7+1/9-1/11+…,计算π的近似值,当最后一项的绝对值小于0.000001为止
  • fpga系列 HDL: 05 阻塞赋值(=)与非阻塞赋值(<=)
  • 大白话DC3算法
  • 力扣HOT100 - 75. 颜色分类
  • Vue.js - 计算属性与侦听器 【0基础向 Vue 基础学习】
  • 技术速递|使用 C# 集合表达式重构代码
  • 我的世界开服保姆级教程
  • [转载]同一台电脑同时使用GitHub和GitLab
  • 【网络协议】【OSI】一次HTTP请求OSI工作过程详细解析
  • springboot vue 开源 会员收银系统 (2) 搭建基础框架
  • Java进阶学习笔记26——包装类
  • 【JavaEE进阶】——要想代码不写死,必须得有spring配置(properties和yml配置文件)
  • 第十四 Elasticsearch介绍和安装
  • YOLOv10介绍与推理--图片和视频演示(附源码)
  • Java实验08
  • MyBatis复习笔记
  • HTML的基石:区块标签与小语义标签的深度解析
  • Windows域控简介
  • 项目延期,不要随意加派人手
  • 帝国CMS验证码不显示怎么回事呢?
  • 【必会面试题】Redis 中的 zset数据结构
  • 括号匹配数据结构
  • c语言:strcmp
  • 传统关系型数据库与hive的区别
  • windows-386、windows-amd64、windows-arm64这三者有什么区别?