9

I'm thinking of saving a camera position (specifically rotation) so changing back and forth between 2D and 3D will always go back to my last viewed position in 3D. What is the best way to do this?

I would also like to save it as a cookie or in local storage so that the user would go straight into that saved view on the page with Cesium from other pages (which may not be the US).

gman
  • 100,619
  • 31
  • 269
  • 393
bcm
  • 5,470
  • 10
  • 59
  • 92

2 Answers2

8

I would recommend just creating a simple JS object and storing the properties of the camera before you switch views. Then just store it into localStorage. I find store.js a really useful wrapper for browser agnostic local storage.

If you need more help I can come up with an example later tonight.

    var camera = $scope.viewer.scene.camera;
    var store = {
      position: camera.position.clone(),
      direction: camera.direction.clone(),
      up: camera.up.clone(),
      right: camera.right.clone(),
      transform: camera.transform.clone(),
      frustum: camera.frustum.clone()
    };
Mike LP
  • 671
  • 8
  • 13
  • Hi @Mike LP, I found this helpful to save and restore the camera settings, have you any clue on how to get world coordinate position (longitude, latitude) directly from the camera setting? I tried something like: ``` Cesium.Cartographic.fromCartesian(viewer.camera.pickEllipsoid(amera.position.clone().x, amera.position.clone().y))``` but seems the wrong approach. Thanks for any help. – epifanio Feb 16 '17 at 15:04
  • off the top of my head, no, but I'll play around with it this weekend – Mike LP Feb 17 '17 at 22:42
  • To get long/lat from the camera position, do the following: var camera = viewer.scene.camera; var currentCartesian = camera.position.clone() var currentCartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(currentCartesian); // then you can use: Cesium.Math.toDegrees(currentCartographic.longitude) etc – kujosHeist Mar 16 '17 at 12:05
2

Based off of Mike LP's answer:

let undoCamera = {
    position: this.camera.position.clone(),
    direction: this.camera.direction.clone(),
    up: this.camera.up.clone()
};
this.camera.position = undoCamera.position;
this.camera.direction = undoCamera.direction;
this.camera.up = undoCamera.up;

This works for me, at least in 3D and Columbus modes.

You can also do the same with frustum, if needed.

https://www.cesium.com/docs/cesiumjs-ref-doc/Camera.html

Andrew
  • 5,839
  • 1
  • 51
  • 72