铯 - 在3D视图中将相机飞到地球的另一半

时间:2017-04-06 08:12:11

标签: camera cesium

有没有办法在Cesium 3D视图中使用相机来查看地球的另一半?

1 个答案:

答案 0 :(得分:2)

我认为你正在寻找这样的事情 1 - 获取当前位置
2 - 计算目的地
3 - flyTo()目的地

现场演示:http://cesiumjs.org/Cesium/Apps/Sandcastle/?src=Hello%20World.html&label=Showcases&gist=4c6e1b30ae619b8e5cba621f4f12f59d

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
    };
}