测距实现总结
开发过程中,有一个需求是测距实时让文本居中显示。参考cesium的API总结如下代码实现测距显示距离。
文本API
动态绘制API
在编写的时候,需要理解的是CallbackProperty这个回调函数,建议去看看api文档或技术网站文章对它有个大概认识。在集成两个API代码遇到最大的问题就是销毁模型问题。大概率就是我这边个人原因,经常报错,经过多次调试,发现是自己通过常用的移除并不能移除模型所导致的。当一个模型被销毁时,该回调函数也跟着销毁。如果销毁模型失败,建议使用removeById去销毁。
/*
@activeShape 动态线
@floatingPoint 点模型
@startPog 开始点坐标 用于计算距离
@labels 动态文本模型
*/
var activeShapePoints = [];
var activeShape;
var floatingPoint;
let startPog;
var scratch = new Cesium.Cartographic();
var geodesic = new Cesium.EllipsoidGeodesic();
let isConstant = false;
var labels;
//创建点
function createP(worldPosition) {
var point = viewer.entities.add({
position: worldPosition,
point: {
color: Cesium.Color.WHITE,
pixelSize: 5,
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
},
});
return point;
}
//划线
function drawShape(positionData) {
var shape;
shape = viewer.entities.add({
polyline: {
positions: positionData,
clampToGround: true,
width: 3,
},
});
return shape;
}
// 动态获取距离
function getLength(time, result) {
// Get the end position from the polyLine's c