业务描述:在场景中添加一个车辆,并实时改变车辆的位置,可选方案比较多,根据实际情况有不同的实现方式。
1、知道车辆所有轨迹的点坐标,用entity方式添加模型,position用SampledPositionProperty记录每个点的时间和位置,并设置好clock。
2、知道车辆所有轨迹的点坐标,采用CZML方式
3、只知道当前位置的坐标。此种情况有两种方式实现:
- 用entity方式添加模型,position用CallbackProperty方式实时改变。应用场景:摄像头实时采集车辆信息,使用webSocket的方式把位置推送到客户端。
- 用entity方式添加模型,position用SampledProperty方式添加样例点。应用场景:摄像头实时采集车辆信息,使用webSocket的方式把位置推送到客户端。此种方式见:cesium 加载gltf并实时改变位置 SampledProperty方式-CSDN博客
此处只实现第三种方式的
viewer.entities.add({
// 位置
position: new Cesium.CallbackProperty(() => {
return new Cesium.Cartesian3.fromDegrees(
lnglat[0], lnglat[1]
)
}, false),
// 设置heading
orientation: new Cesium.CallbackProperty(() => {
return Cesium.Transforms.headingPitchRollQuaternion(
new Cesium.Cartesian3.fromDegrees(
lnglat[0], lnglat[1]
),
new Cesium.HeadingPitchRoll(
Cesium.Math.toRadians(heading),
)
)
}, false),
model: {
uri: "./data/car.gltf",
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
}
});
代码解释:
- webSocket每推送一帧,lnglat和heading 就实时改变