我正在考虑保存相机位置(特别是旋转),因此在 2D 和 3D 之间来回更改将始终返回到我上次在 3D 中查看的位置。最好的方法是什么?
我还想将其保存为 cookie 或本地存储,以便用户可以直接进入其他页面(可能不是美国)的 Cesium 页面上保存的视图。
我建议在切换视图之前创建一个简单的 JS 对象并存储相机的属性。然后将其存储到 localStorage 中即可。我发现 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的回答:
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
执行相同操作。
// Get the current camera view extent (rectangle)
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.");
}