meen
meen

Reputation: 2339

Cesium - Fly camera to other half of earth in 3D view

Is there any way to fly camera to view the other half of the earth in Cesium 3D view?

Upvotes: 0

Views: 718

Answers (1)

Zac
Zac

Reputation: 2211

I think you are looking to do something like this.
1 - Get the current position
2 - Calculate the destination
3 - flyTo() the destination

Live Demo: 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
    };
}

Upvotes: 3

Related Questions