cesium 方位角测量绘线
时间: 2023-11-12 22:08:24 浏览: 114
Cesium中,可以使用以下步骤进行方位角测量绘线:
1.设置操作模式为“drawPloy”。
2.创建一个空数组tempPoints来存储绘制的点。
3.使用Cesium.ScreenSpaceEventHandler监听场景的左击事件,获取点击位置的经纬度坐标,并将其添加到tempPoints数组中。
4.根据tempPoints数组中的点绘制线段和点。
5.计算线段的方位角并显示在屏幕上。
以下是示例代码:
```javascript
var viewer = new Cesium.Viewer('cesiumContainer');
var handler;
var tempPoints = [];
// 设置操作模式为“drawPloy”
function SetMode(mode) {
if (mode == "drawPloy") {
tempPoints = [];
handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handler.setInputAction(function(click) {
var cartesian = viewer.camera.pickEllipsoid(click.position, viewer.scene.globe.ellipsoid);
if (cartesian) {
var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
var longitudeString = Cesium.Math.toDegrees(cartographic.longitude);
var latitudeString = Cesium.Math.toDegrees(cartographic.latitude);
tempPoints.push({
lon: longitudeString,
lat: latitudeString
});
var tempLength = tempPoints.length;
drawPoint(tempPoints[tempPoints.length - 1]);
if (tempLength > 1) {
drawLine(tempPoints[tempPoints.length - 2], tempPoints[tempPoints.length - 1], true);
var azimuth = calculateAzimuth(tempPoints[tempPoints.length - 2], tempPoints[tempPoints.length - 1]);
showAzimuth(azimuth, tempPoints[tempPoints.length - 1]);
}
}
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
}
}
// 绘制点
function drawPoint(point) {
var pointEntity = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(point.lon, point.lat),
point: {
pixelSize: 10,
color: Cesium.Color.YELLOW
}
});
}
// 绘制线段
function drawLine(startPoint, endPoint, isTemp) {
var lineEntity = viewer.entities.add({
polyline: {
positions: [Cesium.Cartesian3.fromDegrees(startPoint.lon, startPoint.lat), Cesium.Cartesian3.fromDegrees(endPoint.lon, endPoint.lat)],
width: 5,
material: isTemp ? Cesium.Color.RED : Cesium.Color.GREEN
}
});
}
// 计算方位角
function calculateAzimuth(startPoint, endPoint) {
var startLat = Cesium.Math.toRadians(startPoint.lat);
var startLon = Cesium.Math.toRadians(startPoint.lon);
var endLat = Cesium.Math.toRadians(endPoint.lat);
var endLon = Cesium.Math.toRadians(endPoint.lon);
var y = Math.sin(endLon - startLon) * Math.cos(endLat);
var x = Math.cos(startLat) * Math.sin(endLat) - Math.sin(startLat) * Math.cos(endLat) * Math.cos(endLon - startLon);
var azimuth = Math.atan2(y, x);
azimuth = Cesium.Math.toDegrees(azimuth);
azimuth = (azimuth + 360) % 360;
return azimuth;
}
// 显示方位角
function showAzimuth(azimuth, endPoint) {
var azimuthEntity = viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(endPoint.lon, endPoint.lat),
label: {
text: 'Azimuth: ' + azimuth.toFixed(2),
font: '16px sans-serif',
fillColor: Cesium.Color.YELLOW,
outlineColor: Cesium.Color.BLACK,
outlineWidth: 2,
style: Cesium.LabelStyle.FILL_AND_OUTLINE,
pixelOffset: new Cesium.Cartesian2(0, -20),
eyeOffset: new Cesium.Cartesian3(0, 0, -100)
}
});
}
```
阅读全文