Bootstrap

CesiumJS 案例 P10:带边框的图片图层(Entity polyline 实现、Entity polygon 实现、Entity rectangle 实现)

CesiumJS

  • CesiumJS API:https://cesium.com/learn/cesiumjs/ref-doc/index.html

  • CesiumJS 是一个开源的 JavaScript 库,它用于在网页中创建和控制 3D 地球仪(地图)


一、带边框的图片图层(Entity polyline)

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>ImageryProvider - 带边框的图片图层(Entity polyline)</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 imageWidth = 200; // 单位为米
		const imageHeight = 100; // 单位为米

		// 图片图层的原点
		const originLongitude = 0; // 原点经度
		const originLatitude = 0; // 原点纬度

		// 在赤道上,每度大约对应 111,320 米
		const degreesPerMeterAtEquator = 111320;

		const longitudeRange = imageWidth / degreesPerMeterAtEquator;
		const latitudeRange = imageHeight / degreesPerMeterAtEquator;

		const west = originLongitude; // 西经(西经为负)
		const south = originLatitude - latitudeRange; // 南纬(南纬为负)
		const east = originLongitude + longitudeRange; // 东经(东经为正)
		const north = originLatitude; // 北纬(北纬为正)

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

		viewer.imageryLayers.addImageryProvider(imageryProvider);

		// 添加边框(西南 -> 西北 -> 东北 -> 东南 -> 西南)
		viewer.entities.add({
			polyline: {
				positions: Cesium.Cartesian3.fromDegreesArray([west, south, west, north, east, north, east, south, west, south]),
				width: 5,
				material: Cesium.Color.RED,
			},
		});

		// 调整相机视角以查看图片
		viewer.camera.flyTo({
			destination: Cesium.Cartesian3.fromDegrees(originLongitude, originLatitude, 1000.0),
			duration: 2.0,
		});
	</script>
</html>

二、带边框的图片图层(Entity polygon)

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>ImageryProvider - 带边框的图片图层(Entity polygon)</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 imageWidth = 200; // 单位为米
		const imageHeight = 100; // 单位为米

		// 图片图层的原点
		const originLongitude = 0; // 原点经度
		const originLatitude = 0; // 原点纬度

		// 在赤道上,每度大约对应 111,320 米
		const degreesPerMeterAtEquator = 111320;

		const longitudeRange = imageWidth / degreesPerMeterAtEquator;
		const latitudeRange = imageHeight / degreesPerMeterAtEquator;

		const west = originLongitude; // 西经(西经为负)
		const south = originLatitude - latitudeRange; // 南纬(南纬为负)
		const east = originLongitude + longitudeRange; // 东经(东经为正)
		const north = originLatitude; // 北纬(北纬为正)

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

		viewer.imageryLayers.addImageryProvider(imageryProvider);

		// 添加边框(西南 -> 西北 -> 东北 -> 东南)
		viewer.entities.add({
			polygon: {
				hierarchy: Cesium.Cartesian3.fromDegreesArray([west, south, west, north, east, north, east, south]),
				material: Cesium.Color.WHITE.withAlpha(0),
				outline: true,
				outlineColor: Cesium.Color.RED,
				outlineWidth: 20,
				height: 0,
			},
		});

		// 调整相机视角以查看图片
		viewer.camera.flyTo({
			destination: Cesium.Cartesian3.fromDegrees(originLongitude, originLatitude, 1000.0),
			duration: 2.0,
		});
	</script>
</html>

三、带边框的图片图层(Entity rectangle)

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>ImageryProvider - 带边框的图片图层(Entity rectangle)</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 imageWidth = 200; // 单位为米
		const imageHeight = 100; // 单位为米

		// 图片图层的原点
		const originLongitude = 0; // 原点经度
		const originLatitude = 0; // 原点纬度

		// 在赤道上,每度大约对应 111,320 米
		const degreesPerMeterAtEquator = 111320;

		const longitudeRange = imageWidth / degreesPerMeterAtEquator;
		const latitudeRange = imageHeight / degreesPerMeterAtEquator;

		const west = originLongitude; // 西经(西经为负)
		const south = originLatitude - latitudeRange; // 南纬(南纬为负)
		const east = originLongitude + longitudeRange; // 东经(东经为正)
		const north = originLatitude; // 北纬(北纬为正)

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

		viewer.imageryLayers.addImageryProvider(imageryProvider);

		// 添加边框
		viewer.entities.add({
			rectangle: {
				coordinates: Cesium.Rectangle.fromDegrees(west, south, east, north),
				material: Cesium.Color.WHITE.withAlpha(0),
				outline: true,
				outlineColor: Cesium.Color.RED,
				outlineWidth: 20,
				height: 0,
			},
		});

		// 调整相机视角以查看图片
		viewer.camera.flyTo({
			destination: Cesium.Cartesian3.fromDegrees(originLongitude, originLatitude, 1000.0),
			duration: 2.0,
		});
	</script>
</html>
;