Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cesium Entities SamplePositionProperty
<div id="cesiumContainer" class="fullScreen">
<div>
var viewer = new Cesium.Viewer('cesiumContainer', {
infoBox: false
});
var entities = viewer.entities;
var duration = 100; //seconds
var frequency = 100; //hertz
var start = Cesium.JulianDate.fromDate(new Date());
var stop = Cesium.JulianDate.addSeconds(start, duration, new Cesium.JulianDate());
viewer.clock.startTime = start.clone();
viewer.clock.stopTime = stop.clone();
viewer.clock.currentTime = start.clone();
viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP; //Loop at the end
viewer.clock.multiplier = 10;
viewer.timeline.zoomTo(start, stop);
var point = {
lat: 40.0,
lon: -75.0
};
var finalPoint = {
lat: 40.0,
lon: -175.0
};
//generate a collection of position samples
var positions = calculatePositionSamples(point, finalPoint, start, duration, frequency);
//create the entity
var target = entities.add({
//Use our computed positions
position: positions,
//Automatically compute orientation based on position movement.
orientation: new Cesium.VelocityOrientationProperty(positions),
//Show the path
path: {
resolution: 1,
material: new Cesium.PolylineGlowMaterialProperty({
glowPower: 0.1,
color: Cesium.Color.RED
}),
width: 5,
trailTime: duration,
leadTime: 0
}
});
viewer.zoomTo(viewer.entities);
viewer.trackEntity = target;
/**
* @param point {{lat:number, lon:number }}
* @param endPoint {{lat:number, lon:number }}
* @param startTime {Cesium.JulianDate}
* @param duration {number} Seconds to calculate for
* @param intervalCount {number}
* @return property {Cesium.SampledPositionProperty}
*/
function calculatePositionSamples(point, endPoint, startTime, duration, intervalCount) {
var property = new Cesium.SampledPositionProperty();
var deltaStep = duration / (intervalCount > 0 ? intervalCount : 1);
var delta = {
lon: (endPoint.lon - point.lon) / intervalCount,
lat: (endPoint.lat - point.lat) / intervalCount
};
for (var since = 0; since <= duration; since += deltaStep) {
property.addSample(
Cesium.JulianDate.addSeconds(startTime, since, new Cesium.JulianDate()),
Cesium.Cartesian3.fromDegrees(point.lon += delta.lon, point.lat += delta.lat)
);
}
return property;
}
<script src="http://cesiumjs.org/Cesium/Build/Cesium/cesium.js"></script>
.fullScreen {
display: block;
position: absolute;
top: 0;
left: 0;
border: none;
width: 100%;
height: 100%;
}
<link href="http://cesiumjs.org/Cesium/Build/Cesium/Widgets/widgets.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment