Cesium 轨迹回放

效果

第一步

创建线的实体 

class Fly {
    constructor(viewer, data) {
        this.viewer = viewer;
        this.data = data;
        this.initLocus();
    }

    initLocus() {
        const datasoure = new Cesium.CustomDataSource('entiestestdata');
        this.viewer.dataSources.add(datasoure);
        const pathData = this.generatePathData(this.data);
        this.addPathToDatasource(datasoure, pathData);

    }
    generatePathData(data) {
        let pathData = [];
        data.forEach((item) => {
            pathData.push(item[0], item[1], item[2]);
        });
        return pathData;
    }
    addPathToDatasource(datasoure, pathData) {
        datasoure.entities.add({
            name: "line",
            polyline: {
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(pathData),
                material: Cesium.Color.YELLOW,
                width: 1,
                heightReference: Cesium.HeightReference.NONE,
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
            },
        });
    }
}

export default Fly;

第二步

添加运动对象属性 核心api是 SampledPositionProperty 

SampledPositionProperty  主要是用来表示物体的动态位置变化  

interpolationDegree 值得大小好像是影响效率和精度的

class Fly {
    constructor(viewer, data) {
        this.viewer = viewer;
        this.data = data;
        this.initLocus();
    }

    initLocus() {
        const datasoure = new Cesium.CustomDataSource('entiestestdata');
        this.viewer.dataSources.add(datasoure);
        const pathData = this.generatePathData(this.data);
        this.addPathToDatasource(datasoure, pathData);

        const property = this.createPositionProperty(this.data);

    }
    generatePathData(data) {
        let pathData = [];
        data.forEach((item) => {
            pathData.push(item[0], item[1], item[2]);
        });
        return pathData;
    }
    addPathToDatasource(datasoure, pathData) {
        datasoure.entities.add({
            name: "line",
            polyline: {
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(pathData),
                material: Cesium.Color.YELLOW,
                width: 1,
                heightReference: Cesium.HeightReference.NONE,
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
            },
        });
    }
    createPositionProperty(data) {
        let property = new Cesium.SampledPositionProperty();
        data.forEach((item, index) => {
            let time = new Date(Date.now() + index * 2000);
            let position = Cesium.Cartesian3.fromDegrees(item[0], item[1], item[2]);
            property.addSample(Cesium.JulianDate.fromDate(time), position);
            this.viewer.entities.add({
                position: position,
                point: {
                    pixelSize: 10,
                    color: Cesium.Color.RED,
                    outlineWidth: 2,
                    outlineColor: Cesium.Color.DARKRED,
                },
            });
        });
        property.setInterpolationOptions({
            interpolationDegree: 0.01,
            interpolationAlgorithm: Cesium.LagrangePolynomialApproximation,
        });
        return property;
    }
}

export default Fly;

第三步(完整代码)

添加模型

orientation 计算朝向的api

clock.currentTime 开始时间

clock.stopTime 结束时间

 this.viewer.clock.clockRange 几个属性

CLAMPED:达到终止时间后停止

LOOP_STOP:达到终止时间后重新循环

UNBOUNDED:达到终止时间后继续读秒(默认)

clock.shouldAnimate true是开始 赋值为false则结束

class Fly {
    constructor(viewer, data) {
        this.viewer = viewer;
        this.data = data;
        this.initLocus();
    }

    initLocus() {

        const datasoure = new Cesium.CustomDataSource('entiestestdata');
        this.viewer.dataSources.add(datasoure);

        const pathData = this.generatePathData(this.data);
        this.addPathToDatasource(datasoure, pathData);
        const property = this.createPositionProperty(this.data);
        const dates = this.getStopTime(this.data);

        this.addEntityWithModelAndAnimation(property, dates, datasoure);
    }
    generatePathData(data) {
        let pathData = [];
        data.forEach((item) => {
            pathData.push(item[0], item[1], item[2]);
        });
        return pathData;
    }

    addPathToDatasource(datasoure, pathData) {
        datasoure.entities.add({
            name: "line",
            polyline: {
                positions: Cesium.Cartesian3.fromDegreesArrayHeights(pathData),
                material: Cesium.Color.YELLOW,
                width: 1,
                heightReference: Cesium.HeightReference.NONE,
                disableDepthTestDistance: Number.POSITIVE_INFINITY,
            },
        });
    }

    createPositionProperty(data) {
        let property = new Cesium.SampledPositionProperty();
        data.forEach((item, index) => {
            let time = new Date(Date.now() + index * 2000);
            let position = Cesium.Cartesian3.fromDegrees(item[0], item[1], item[2]);
            property.addSample(Cesium.JulianDate.fromDate(time), position);
            this.viewer.entities.add({
                position: position,
                point: {
                    pixelSize: 10,
                    color: Cesium.Color.RED,
                    outlineWidth: 2,
                    outlineColor: Cesium.Color.DARKRED,
                },
            });
        });
        property.setInterpolationOptions({
            interpolationDegree: 0.01,
            interpolationAlgorithm: Cesium.LagrangePolynomialApproximation,
        });
        return property;
    }

    getStopTime(data) {
        const stoptime = new Date(Date.now() + (data.length - 1) * 2000);
        return stoptime;
    }

    addEntityWithModelAndAnimation(property, dates, datasource) {
        this.viewer.entities.add({
            availability: new Cesium.TimeIntervalCollection([
                new Cesium.TimeInterval({
                    start: Cesium.JulianDate.fromDate(new Date()),
                    stop: Cesium.JulianDate.fromDate(dates),
                }),
            ]),
            zIndex: 1,
            position: property,
            orientation: new Cesium.VelocityOrientationProperty(property),
            model: {
                uri: "http://localhost:8080/GroundVehicle.glb",
            },
            path: {
                resolution: 1,
                material: new Cesium.PolylineGlowMaterialProperty({
                    glowPower: 0.1,
                    color: Cesium.Color.YELLOW
                }),
                width: 10
            }

        });
        this.viewer.clock.currentTime = Cesium.JulianDate.fromDate(new Date());
        this.viewer.clock.stopTime = Cesium.JulianDate.fromDate(dates);
        this.viewer.clock.clockRange = Cesium.ClockRange.CLAMPED;
        this.viewer.flyTo(datasource);
        this.viewer.clock.shouldAnimate = true;
    }
}

export default Fly;

  • 17
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值