/**
* @param {Object} startTime Date格式的开始时间
* @param {Object} endTime Date格式的结束时间
* @param {Object} coordinates [x1,y1,x2,y2,x3,y3.......]
* @param {Object} entityCollection 实体收集器
*/
async function interpolationLine(startTime,endTime,coordinates,entityCollection){
try{
var start = Cesium.JulianDate.fromDate(startTime);
var stop = Cesium.JulianDate.fromDate(endTime);
// 计算每个坐标点的时间间隔
var timeIntervals = [];
//按每分钟进行插值
var totalDuration = Cesium.JulianDate.secondsDifference(stop, start)/60;
//生成插值点
var positions = await lerpPostions(coordinates,Math.ceil(totalDuration));
var intervalDuration = totalDuration / (positions.length - 1);
var currentTime = start;
for (var i = 0; i < positions.length; i++) {
var nextTime = Cesium.JulianDate.addDays(currentTime, intervalDuration, new Cesium.JulianDate());
timeIntervals.push(new Cesium.TimeInterval({
start: currentTime,
stop: nextTime,
isStartIncluded: true,
isStopIncluded: i === positions.length - 2 // 最后一个间隔包括结束时间
}));
currentTime = nextTime;
}
let lineps = [];
const postionsLength = positions.length;
entityCollection.entities.add({
polyline: {
clampToGround: true,
positions: new Cesium.CallbackProperty((time)=>{
var halfHour = Cesium.JulianDate.secondsDifference(time, start)/60;
var dateRate = (halfHour/totalDuration);
return positions.slice(0, postionsLength*dateRate);
}, false),
width: 20,
material: new Cesium.PolylineGlowMaterialProperty({
glowPower: 0.1,
color: Cesium.Color.YELLOW
}),
}
});
}catch(error){
console.log("interpolationLine occur Error:",error);
}
}
/**
* 生成均匀随机点坐标
* @param {Array} positions [x1,y1,x2,y2,x3,y3.......]
* @param {Number} number 需要进行插值的数量
* @param {Number} number 生成的随机点数
* @returns 返回生成的随机点坐标集合
*/
async function lerpPostions(positions,number,flag=true){
var timeNumber = [];//占总时间的比例集合
var lineDiatance = 0;//线的总长度
var tempDis = [];//距离
const pointPosition = [];
const points = [];
try{
if (positions.length <= 3) return;
if(!flag){
return positions;
}
//计算线段总长度
for (var i = 0; i < positions.length - 2; i+=2) {
let startPoint = new Cesium.Cartesian3.fromDegrees(positions[i],positions[i+1]);
let currentDistance = Cesium.Cartesian3.distance(
startPoint,
new Cesium.Cartesian3.fromDegrees(positions[i+2],positions[i+3])
);
lineDiatance += currentDistance;
tempDis.push(lineDiatance);
points.push(startPoint);
}
//最后一截线段
var lastPoint = new Cesium.Cartesian3.fromDegrees(positions[positions.length - 2],positions[positions.length - 1]);
let lastDistance = Cesium.Cartesian3.distance(
points[points.length - 1],
lastPoint
);
lineDiatance += lastDistance;
tempDis.push(lineDiatance);
points.push(lastPoint);
//设置每一个到达坐标对应的时间节点
for (let i = 0; i < points.length; i++) {
var everyTime = tempDis[i] / lineDiatance;
timeNumber.push(everyTime);
}
//使用线性插值(线性插值的性能最高,但也最不圆滑)
var spline = new Cesium.LinearSpline({
times: timeNumber,
points: points
});
for (let i = 1; i <= number; i++) {
var cartesian3 = spline.evaluate(i / number);
pointPosition.push(cartesian3);
// 绘制插值点
// var interPoint = viewer.entities.add({
// position: cartesian3,
// point: {
// color: Cesium.Color.YELLOW,
// pixelSize: 6,
// disableDepthTestDistance: Number.POSITIVE_INFINITY
// }
// });
}
return pointPosition
}catch(error){
console.log("lerpPostions occur error:",error);
}
return positions;
}
调用代码如下
//设定了模拟时间的边界
var startTime = new Date("2022-08-21 11:12:13");
var endTime = new Date("2022-08-23 05:11:13");
var start = Cesium.JulianDate.fromDate(startTime);
var stop = Cesium.JulianDate.fromDate(endTime);
//确保查看器处于预期的时间
viewer.clock.startTime = start.clone();
viewer.clock.stopTime = stop.clone();
viewer.clock.currentTime = start.clone();
viewer.clock.clockRange = Cesium.ClockRange.CLAMPED; //循环结束时
//时间变化来控制速度
viewer.clock.multiplier = 360;
//给时间线设置边界
viewer.timeline.zoomTo(start, stop);
//建立一个统一管理实体的收集器
let entityCollection = new Cesium.CustomDataSource("flowEntityCollection");
viewer.dataSources.add(entityCollection);
interpolationLine(startTime,endTime,coordinates,entityCollection);