Cesium JS保存相机位置

9

我想保存一个相机位置(特别是旋转),这样在2D和3D之间来回切换时,总是能回到我上次在3D中查看的位置。最好的方法是什么?

我还希望将其保存为cookie或本地存储,以便用户从其他页面(可能不是US)进入带有Cesium的页面时直接进入该保存视图。

3个回答

8
我建议您只需创建一个简单的JS对象并在切换视图之前存储相机属性。然后将其存储到本地存储中。我发现store.js是一个非常有用的浏览器不可知本地存储包装器。
如果需要更多帮助,我可以在今晚稍后提供一个示例。
    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,我发现这个保存和恢复相机设置的方法很有用,你有没有什么线索可以直接从相机设置中获取世界坐标位置(经度,纬度)?我尝试了一些类似于:Cesium.Cartographic.fromCartesian(viewer.camera.pickEllipsoid(amera.position.clone().x, amera.position.clone().y))但似乎是错误的方法。感谢任何帮助。 - epifanio
我一时半会儿想不出来,但我会在这个周末试着研究一下。 - Mike LP
要从相机位置获取经纬度,请执行以下操作: var camera = viewer.scene.camera; var currentCartesian = camera.position.clone() var currentCartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(currentCartesian); // 然后您可以使用:Cesium.Math.toDegrees(currentCartographic.longitude) 等 - kujosHeist

2

基于Mike LP的回答:

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;

这对我来说是有效的,至少在3D和哥伦布模式下。

如果需要,您也可以使用frustum进行相同操作。

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


0
// 获取当前相机视图范围(矩形) const cameraExtentRectangle = viewer.camera.computeViewRectangle();
// Check if the camera extent is valid (not zero or near-zero)
if (Cesium.defined(cameraExtentRectangle)) {
  const west = Cesium.Math.toDegrees(cameraExtentRectangle.west);
  const south = Cesium.Math.toDegrees(cameraExtentRectangle.south);
  const east = Cesium.Math.toDegrees(cameraExtentRectangle.east);
  const north = Cesium.Math.toDegrees(cameraExtentRectangle.north);

  // Create a GeoJSON Polygon feature from the extent
  const geoJSONFeature = {
    type: "Feature",
    properties: {},
    geometry: {
      type: "Polygon",
      coordinates: [
        [
          [west, north],
          [east, north],
          [east, south],
          [west, south],
          [west, north], // Closing the polygon
        ],
      ],
    },
  };

  // Convert the GeoJSON feature to a JSON string
  const geoJSONString = JSON.stringify(geoJSONFeature);

  console.log(geoJSONString);
} else {
  console.error("Invalid camera extent: Cannot compute the view extent.");
}

我们可以使用这个网站来测试geoJson多边形: https://geojson.io/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接