CesiumJS 案例 P35:添加图片图层(添加图片数据)

CesiumJS

  • CesiumJS 是一个开源的 JavaScript 库,它用于在网页中创建和控制 3D 地球仪(地图)
  1. CesiumJS 官网:https://www.cesium.com/

  2. CesiumJS 下载地址:https://www.cesium.com/platform/cesiumjs/

  3. CesiumJS API 文档:https://cesium.com/learn/cesiumjs/ref-doc/index.html


添加图片图层(添加图片数据)

html 复制代码
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>ImageryProvider - 添加图片图层(添加图片数据)</title>
		<link rel="stylesheet" href="../js/Cesium-1.112/Build/Cesium/Widgets/widgets.css" />
		<style>
			* {
				margin: 0;
				padding: 0;
				box-sizing: border-box;
			}

			html,
			body {
				width: 100%;
				height: 100%;
			}

			.container {
				width: 100%;
				height: 100%;
			}
		</style>
	</head>

	<body>
		<div id="container"></div>
	</body>

	<script src="../js/Cesium-1.112/Build/Cesium/Cesium.js"></script>
	<script>
		const viewer = new Cesium.Viewer("container");

		const west = 0; // 西经(西经为负)
		const south = 0; // 南纬(南纬为负)
		const east = 10; // 东经(东经为正)
		const north = 10; // 北纬(北纬为正)

		imageToBase64("../img/test.jpg", (base64) => {

			// 创建图片图层
			const imageryProvider = new Cesium.SingleTileImageryProvider({
				url: base64,
				rectangle: Cesium.Rectangle.fromDegrees(west, south, east, north),
			});

            const imageryLayer = viewer.imageryLayers.addImageryProvider(imageryProvider);
		});

		function imageToBase64(url, callback) {
			const img = new Image();
			img.crossOrigin = "Anonymous";
			img.onload = function () {
				const canvas = document.createElement("canvas");
				const ctx = canvas.getContext("2d");
				canvas.width = img.width;
				canvas.height = img.height;
				ctx.drawImage(img, 0, 0);

				const base64 = canvas.toDataURL("image/jpeg");
				callback(base64);
			};
			img.src = url;
		}
	</script>
</html>
相关推荐
岁忧4 小时前
GoLang五种字符串拼接方式详解
开发语言·爬虫·golang
tyatyatya4 小时前
MATLAB基础数据类型教程:数值型/字符型/逻辑型/结构体/元胞数组全解析
开发语言·matlab
遇到困难睡大觉哈哈4 小时前
Harmony os 静态卡片(ArkTS + FormLink)详细介绍
前端·microsoft·harmonyos·鸿蒙
用户47949283569155 小时前
Bun 卖身 Anthropic!尤雨溪神吐槽:OpenAI 你需要工具链吗?
前端·openai·bun
p***43485 小时前
前端在移动端中的网络请求优化
前端
心无旁骛~5 小时前
python多进程和多线程问题
开发语言·python
星云数灵5 小时前
使用Anaconda管理Python环境:安装与验证Pandas、NumPy、Matplotlib
开发语言·python·数据分析·pandas·教程·环境配置·anaconda
g***B7385 小时前
前端在移动端中的Ionic
前端
kaikaile19955 小时前
基于遗传算法的车辆路径问题(VRP)解决方案MATLAB实现
开发语言·人工智能·matlab
四问四不知5 小时前
Rust语言进阶(结构体)
开发语言·后端·rust