首先需要注意new Cesium.Viewer的时候需要将
timeline: true,
shouldAnimate: true, // 初始时刻运动
两个属性置为true否则不生效
export class initSatellite {
start: any;
stop: any;
viewer: any;
constructor(option) {
// 赋值实例
this.viewer = option.viewer.value;
this.start = new Cesium.JulianDate.fromDate(new Date());
this.start = Cesium.JulianDate.addHours(
this.start,
8,
new Cesium.JulianDate()
); //东八区时间
// 结束时间
this.stop = Cesium.JulianDate.addSeconds(
this.start,
360,
new Cesium.JulianDate()
);
//确保查看器处于预期的时间
this.viewer.clock.startTime = this.start.clone();
this.viewer.clock.stopTime = this.stop.clone();
this.viewer.clock.currentTime = this.start.clone();
this.viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP; //循环结束时
//时间变化来控制速度 // 时间速率,数字越大时间过的越快
this.viewer.clock.multiplier = 1;
//给时间线设置边界
this.viewer.timeline.zoomTo(this.start, stop);
this.startfunc();
}
mySatePosition() {
let lon = 0;
let lat = 0;
let hei = 700000; //卫星高度
let phei = 700000 / 2; //轨道高度
let time = 10;
return { lon, lat, hei, phei, time };
}
computeCirclularFlight(source, panduan) {
var property = new Cesium.SampledPositionProperty();
if (panduan == 1) {
//卫星位置
for (var i = 0; i < source.length; i++) {
var time = Cesium.JulianDate.addSeconds(
this.start,
source[i].time,
new Cesium.JulianDate()
);
var position = Cesium.Cartesian3.fromDegrees(
source[i].lon,
source[i].lat,
source[i].hei
);
// 添加位置,和时间对应
property.addSample(time, position);
}
} else if (panduan == 2) {
//轨道位置
for (var i = 0; i < source.length; i++) {
var time = Cesium.JulianDate.addSeconds(
this.start,
source[i].time,
new Cesium.JulianDate()
);
var position = Cesium.Cartesian3.fromDegrees(
source[i].lon,
source[i].lat,
source[i].phei
);
// 添加位置,和时间对应
property.addSample(time, position);
}
}
return property;
}
// 位置
getRandState() {
let horzin = {};
var arr = [];
var t1 = Math.floor(Math.random() * 360);
var t2 = Math.floor(Math.random() * 360);
for (var i = t1; i <= 360 + t1; i += 30) {
var aaa = this.mySatePosition();
aaa.lon = t2;
aaa.lat = i;
aaa.time = i - t1;
arr.push(aaa);
}
horzin = arr;
return horzin;
}
getStatePath(aaa) {
var entity_ty1p = this.computeCirclularFlight(aaa, 2);
var entity_ty1 = this.viewer.entities.add({
availability: new Cesium.TimeIntervalCollection([
new Cesium.TimeInterval({
start: this.start,
stop: this.stop,
}),
]),
position: entity_ty1p, //轨道高度
orientation: new Cesium.VelocityOrientationProperty(entity_ty1p),
cylinder: {
HeightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
length: 700000,
topRadius: 0,
bottomRadius: 700000 / 2,
material: Cesium.Color.RED.withAlpha(0.4),
outline: !0,
numberOfVerticalLines: 0,
outlineColor: Cesium.Color.RED.withAlpha(0.8),
},
});
entity_ty1.position.setInterpolationOptions({
interpolationDegree: 5,
interpolationAlgorithm: Cesium.LagrangePolynomialApproximation,
});
var entity1p = this.computeCirclularFlight(aaa, 1);
//创建实体
var entity1 = this.viewer.entities.add({
// 将实体availability设置为与模拟时间相同的时间间隔。
availability: new Cesium.TimeIntervalCollection([
new Cesium.TimeInterval({
start: this.start,
stop: this.stop,
}),
]),
position: entity1p, //计算实体位置属性
//基于位置移动自动计算方向.
orientation: new Cesium.VelocityOrientationProperty(
this.computeCirclularFlight(aaa, 1)
),
//加载飞机模型
model: {
uri: "/sr71.glb",
minimumPixelSize: 32,
},
//路径
path: {
resolution: 1,
material: new Cesium.PolylineGlowMaterialProperty({
glowPower: 0.1,
color: Cesium.Color.PINK,
}),
width: 5,
},
});
//差值器
entity1.position.setInterpolationOptions({
interpolationDegree: 5,
interpolationAlgorithm: Cesium.LagrangePolynomialApproximation,
});
}
startfunc() {
this.getStatePath(this.getRandState());
}
}
使用
new initSatellite({ viewer });