CesiumJS
- CesiumJS 是一个开源的 JavaScript 库,它用于在网页中创建和控制 3D 地球仪(地图)
-
CesiumJS 官网:https://www.cesium.com/
-
CesiumJS 下载地址:https://www.cesium.com/platform/cesiumjs/
-
CesiumJS API 文档:https://cesium.com/learn/cesiumjs/ref-doc/index.html
一、计算距离(Cartesian3)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>距离 - 计算距离(Cartesian3)</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 point1 = Cesium.Cartesian3.fromDegrees(-75.59777, 40.03883);
const point2 = Cesium.Cartesian3.fromDegrees(-76.59777, 41.03883);
const distance = Cesium.Cartesian3.distance(point1, point2);
console.log("距离为(米):", distance);
</script>
</html>
二、计算距离(EllipsoidGeodesic)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>距离 - 计算距离(EllipsoidGeodesic)</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 calculateDistance = (start, end) => {
const geodesic = new Cesium.EllipsoidGeodesic();
geodesic.setEndPoints(start, end);
return geodesic.surfaceDistance;
};
const point1 = Cesium.Cartographic.fromDegrees(-75.59777, 40.03883);
const point2 = Cesium.Cartographic.fromDegrees(-76.59777, 41.03883);
const distance = calculateDistance(point1, point2);
console.log("距离为(米):", distance);
</script>
</html>