Vue集成Cesium之二 —— 相机(Camera)

上一篇文章初步写了一下 vue 集成 cesium 方法和注意的地方。

最近因为项目中用到的地图资源并不是拿来就能用,需要调整显示的角度、缩放等设置。所以把 cesium 所有的相机(也就是视角)设置都体验了一遍,正好记录一下异同,分享出来。

笔者作为初探的小白,如有错漏还请看官指出加以改正。

Viewer.zoomTo(target, offset)

官方解释:
Asynchronously sets the camera to view the provided entity, entities, or data source. If the data source is still in the process of loading or the visualization is otherwise still loading, this method waits for the data to be ready before performing the zoom.
The offset is heading/pitch/range in the local east-north-up reference frame centered at the center of the bounding sphere. The heading and the pitch angles are defined in the local east-north-up reference frame. The heading is the angle from y axis and increasing towards the x axis. Pitch is the rotation from the xy-plane. Positive pitch angles are above the plane. Negative pitch angles are below the plane. The range is the distance from the center. If the range is zero, a range will be computed such that the whole bounding sphere is visible.

翻译成人话:
这个方法用来异步控制相机平面旋转、Z轴旋转和缩放(个人理解)。

首先说一下两个参数:
target:相机显示的焦点,可以是图像、实体等;
offset:就是上面说的旋转角度和缩放距离,这里接收一个 HeadingPitchRange 对象。

示例一:不做旋转设置

viewer.zoomTo(
  tileset, //通过 new Cesium3DTileset 创建的图像,使相机聚焦图像位置
  new Cesium.HeadingPitchRange(0,0,0) //旋转缩放配置,默认为0不做旋转缩放
);

在这里插入图片描述

示例二:沿Z轴旋转

可以看到初始的图像不适合展示使用,那么首先旋转地图以看到正面。

viewer.zoomTo(
  tileset,//通过 new Cesium3DTileset 创建的图像,使相机聚焦图像位置
  new Cesium.HeadingPitchRange( //旋转缩放配置,默认为0不做旋转缩放
    0,
    -0.5,//Z轴旋转角度
    0
  )
);

在这里插入图片描述

示例二:缩放设置

通过旋转图像可以看到地图的全景了,但是项目需求我们显示某个区域的图像,而不是现在这种满屏黑色背景。

所以需要设置缩放,这里缩放不同于高德百度那种设置缩放的级别,这里是通过设置相机的显示距离进行缩放。

viewer.zoomTo(
  tileset,//通过 new Cesium3DTileset 创建的图像,使相机聚焦图像位置
  new Cesium.HeadingPitchRange( //旋转缩放配置,默认为0不做旋转缩放
    0,
    -0.5,//Z轴旋转角度
    500//设置显示距离
  )
);

在这里插入图片描述

示例三:平面旋转(或Y轴旋转)

viewer.zoomTo(
  tileset,//通过 new Cesium3DTileset 创建的图像,使相机聚焦图像位置
  new Cesium.HeadingPitchRange( //旋转缩放配置,默认为0不做旋转缩放
    0.5,
    -0.5,//Z轴旋转角度
    500//设置显示距离
  )
);

在这里插入图片描述
可以看到相较于前面的视角,逆时针旋转。如果这里看的不明显,可以通过鼠标控制相机高度。可以对比示例二中图像的方向,就比较明显了。
在这里插入图片描述

Viewer.flyTo(target, options)

官方解释:
Flies the camera to the provided entity, entities, or data source. If the data source is still in the process of loading or the visualization is otherwise still loading, this method waits for the data to be ready before performing the flight.
The offset is heading/pitch/range in the local east-north-up reference frame centered at the center of the bounding sphere. The heading and the pitch angles are defined in the local east-north-up reference frame. The heading is the angle from y axis and increasing towards the x axis. Pitch is the rotation from the xy-plane. Positive pitch angles are above the plane. Negative pitch angles are below the plane. The range is the distance from the center. If the range is zero, a range will be computed such that the whole bounding sphere is visible.

感觉跟 zoomTo 方法的解释没有,太大区别。唯一的区别我感觉就是同步异步的问题,zoomTo 官方说明是异步设置相机角度,具体我们感受太出来,因为加载都挺慢的。

但是 flyTo 方法肯定是同步的没毛病,因为它的 options 有一个参数 duration ,是相机视角转移到指定图像的时间。并且实际开发中可以看出来视角转移的过程,是图像首先加载出来后,再转移相机视角。

option

配置项有三个:
duration:相机移动到指定的图像用时,默认3秒
maximumHeight:限制相机最大高度,不过我尝试了几种设置还没有说明效果,并没有影响图像显示
offset:接收一个 HeadingPitchRange 对象,同zoomTo方法的效果

示例一 无配置项


viewer.flyTo(
  tileset //通过 new Cesium3DTileset 创建的图像,使相机聚焦图像位置
)

在这里插入图片描述
可以看到方法自动设置了相机的旋转和缩放,不再是初始的图像。

示例二 配置旋转缩放

viewer.flyTo(
  tileset, //通过 new Cesium3DTileset 创建的图像,使相机聚焦图像位置
  {
     duration:1,
     maximumHeight:0.5,
     offset:new Cesium.HeadingPitchRange(
       0.5,
       -0.5,
       500
     )
   }
)

在这里插入图片描述
运行后的效果和 zoomTo 方法一致,处理有1秒的移动动画,其它没有区别。

所以 flyTo 和 zoomTo 两个方法看起来差别就在同步和异步上,各位根据自己需求取舍。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将Cesium 1.105集成Vue中,需要执行以下步骤: 1. 在Vue项目中安装Cesium库。可以通过npm或yarn来安装,执行以下命令: ```shell npm install cesium@1.105 --save ``` 或 ```shell yarn add cesium@1.105 ``` 2. 在Vue项目的`src`目录下创建一个`assets`文件夹,并将以下文件复制到此文件夹下: - `node_modules/cesium/Build/Cesium/Cesium.js` - `node_modules/cesium/Build/Cesium/Widgets/widgets.css` 3. 在Vue项目的`public`文件夹下创建一个`js`文件夹,并将以下文件复制到此文件夹下: - `node_modules/cesium/Build/Cesium/Workers` 4. 在Vue项目的`public`文件夹下的`index.html`文件中,添加以下代码段到`head`标签中: ```html <link rel="stylesheet" href="%PUBLIC_URL%/js/widgets/widgets.css" /> <script src="%PUBLIC_URL%/js/Cesium.js"></script> ``` 5. 在Vue项目的`src`目录下创建一个`components`文件夹,并创建一个`CesiumViewer.vue`文件。在此文件中,可以使用以下示例代码来创建一个Cesium Viewer组件并展示地球: ```vue <template> <div id="cesiumContainer" style="width: 100%; height: 100%;"></div> </template> <script> export default { mounted() { const viewer = new Cesium.Viewer('cesiumContainer'); } }; </script> <style scoped> #cesiumContainer { position: absolute; top: 0; left: 0; bottom: 0; right: 0; } </style> ``` 6. 在Vue项目的App.vue文件或其他组件中,使用以下代码导入并使用CesiumViewer组件: ```vue <template> <div id="app"> <CesiumViewer /> </div> </template> <script> import CesiumViewer from './components/CesiumViewer.vue'; export default { components: { CesiumViewer } }; </script> ``` 完成以上步骤后,Cesium 1.105就可以在Vue项目中使用了。运行Vue项目后,将展示一个带有Cesium Viewer的地球场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值