CESIUM例子学习(四)——CallbackProperty

cesium号称是集显示时空数据于一体的三维引擎。空间数据的展示我们已经见到,对于时间上的数据,我觉得CallbackProperty是最大功臣。因为使用CallbackProperty,cesium中一切可视化的要素都可以与时间联系起来。

一、定义CallbackProperty函数

定义CallbackProperty函数代码如下:

new Cesium.CallbackProperty(getEndPoint, isConstant)
function getEndPoint (time, result) {
    endLongitude = startLongitude - 0.001 * Cesium.JulianDate.secondsDifference(time, startTime);
    return Cesium.Cartesian3.fromDegreesArray(
        [startLongitude, startLatitude, endLongitude, startLatitude],
        Cesium.Ellipsoid.WGS84,
        result
    );
}

CallbackProperty(getEndPoint, isConstant)中,isConstant是用来判断函数返回的值是否要更新到渲染中去。判断的标准是否很简单,就是预先指定返回的值是变还是不变的。例如代码中每次返回的坐应该都是变的,所以应该设置成false,也是就,返回的值是连续变化的。当返回不变的值时,即可不更新渲染数据(从实验得到的结果是这样的,不知道理解是否正确)。

二、CallbackProperty应用实例

下面的代码实例就是利用CallbackProperty函数获不断更新线段的终点,并在线段中点位置显示线段的长度。

var startLatitude = 35;
var startLongitude = 120;
var endLongitude;
var startTime = Cesium.JulianDate.now();
var isConstant = false;
// use scratch object to avoid new allocations per frame.
var endCartographic = new Cesium.Cartographic();
var scratch = new Cesium.Cartographic();
var geodesic = new Cesium.EllipsoidGeodesic();
var startCartographic = Cesium.Cartographic.fromDegrees(
    startLongitude,
    startLatitude
);
var redLine;
function addLine () {
    redLine = viewer.entities.add({
        polyline: {
            // This callback updates positions each frame.
            positions: new Cesium.CallbackProperty(getEndPoint, false),
            width: 5,
            material: Cesium.Color.RED,
        },
    });
    addLabel()
    viewer.flyTo(redLine)
}
function getEndPoint (time, result) {
    endLongitude = startLongitude - 0.001 * Cesium.JulianDate.secondsDifference(time, startTime);
    return Cesium.Cartesian3.fromDegreesArray(
        [startLongitude, startLatitude, endLongitude, startLatitude],
        Cesium.Ellipsoid.WGS84,
        result
    );
}
// Calculate the length of the line
function getLength (time, result) {
    // Get the end position from the polyLine's callback.
    var endPoint = redLine.polyline.positions.getValue(time, result)[1];
    endCartographic = Cesium.Cartographic.fromCartesian(endPoint);
    geodesic.setEndPoints(startCartographic, endCartographic);
    var lengthInMeters = Math.round(geodesic.surfaceDistance);
    return (lengthInMeters / 1000).toFixed(1) + " km";
}
// Label the polyline with calculated length.
function addLabel () {
    var label = viewer.entities.add({
        position: new Cesium.CallbackProperty(getMidpoint, isConstant),
        label: {
            // This callback updates the length to print each frame.
            text: new Cesium.CallbackProperty(getLength, isConstant),
            font: "20px sans-serif",
            pixelOffset: new Cesium.Cartesian2(0.0, 20),
        },
    });
}

function getMidpoint (time, result) {
    // Get the end position from the polyLine's callback.
    var endPoint = redLine.polyline.positions.getValue(time, result)[1];
    endCartographic = Cesium.Cartographic.fromCartesian(endPoint);

    geodesic.setEndPoints(startCartographic, endCartographic);
    var midpointCartographic = geodesic.interpolateUsingFraction(
        0.5,
        scratch
    );
    return Cesium.Cartesian3.fromRadians(
        midpointCartographic.longitude,
        midpointCartographic.latitude
    );
}

显示结果如下图:

 

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值