有没有办法在Cesium 3D视图中使用相机来查看地球的另一半?
答案 0 :(得分:2)
我认为你正在寻找这样的事情
1 - 获取当前位置
2 - 计算目的地
3 - flyTo()
目的地
var viewer = new Cesium.Viewer('cesiumContainer');
var scene = viewer.scene;
var btn = document.getElementById('swap');
btn.addEventListener('click', goToOtherSide);
function goToOtherSide() {
// Get the position of the camera
var currentPosition = getPosition();
// Get the position on the other side of the earth
var nextPosition = getOtherSide(currentPosition);
// Use flyTo with the new position, and maintain the height
viewer.camera.flyTo({
destination : Cesium.Cartesian3.fromDegrees(+nextPosition.lon, +nextPosition.lat, viewer.camera.positionCartographic.height)
});
}
function getPosition() {
var center = viewer.camera.pickEllipsoid(
new Cesium.Cartesian2(viewer.canvas.width / 2, viewer.canvas.height / 2), Cesium.Ellipsoid.WGS84);
var cartographic = scene.globe.ellipsoid.cartesianToCartographic(center);
var lat = Cesium.Math.toDegrees(cartographic.latitude).toFixed(2);
var lon = Cesium.Math.toDegrees(cartographic.longitude).toFixed(2);
return {
lat: lat,
lon: lon
};
}
function getOtherSide(currentPosition) {
var ln = +currentPosition.lon + 180;
if (ln > 180) {
ln -= 360;
}
var lt = -currentPosition.lat;
return {
lat: lt,
lon: ln
};
}