cesium介绍
cesium
- Cesium 是一款面向三维地球和地图的,世界级的JavaScript开源产品。它提供了基于JavaScript语言的开发包,方便用户快速搭建一款零插件的虚拟地球Web应用,并在性能,精度,渲染质量以及多平台,易用性上都有高质量的保证。
导入
"cesium": "1.79.1",
"cesium-navigation-es6": "1.1.7",
"copy-webpack-plugin": "5.1.2",
首先就是使用npm或者yarn安装这两个依赖包,第一个是cesium的js全量包。第二个用于使用es6的语法,第三个是将cesium的静态文件映射到工程文件中。
声明
import '../node_modules/cesium/Build/Cesium/Widgets/widgets.css';
这是将cesium中的样式文件导入到项目中。
配置
chainWebpack: config => {
config.plugin('copy').use(require('copy-webpack-plugin'), [[
{
from: path.resolve('node_modules/cesium/Build/Cesium'),
to: './cesium'
},
{
from: path.resolve('./public'),
to: './cesium/public'
}
]]);
},
将cesium的资源拷贝至本项目中,使用模型的时候不会导致出现路径找不到的问题
使用
在需要使用的页面进行配置即可。
<div id="cesiumContainer" class="fullSize"></div>
<div class="mouse-detail">
<div> 经度:{{ positionDetail.longitude }}</div>
<div> 纬度:{{ positionDetail.latitude }}</div>
<div> 高程:{{ positionDetail.altitude }}</div>
<div> 视高:{{ positionDetail.height }}</div>
</div>
.fullSize {
width: 100%;
height: 100%;
}
.mouse-detail {
position: absolute;
bottom: 0px;
left: 5px;
display: flex;
color: white;
}
interface positionInfo {
longitude: string;
latitude: string;
altitude: string;
height: string;
}
import {Viewer} from 'cesium';
viewer: Viewer;
positionDetail: positionInfo = {
longitude: '',
latitude: '',
altitude: '',
height: ''
};
mounted() {
this.mapInit('cesiumContainer');
this.viewer._cesiumWidget._creditContainer.style.display = 'none';
this.viewer._toolbar.style.display = 'none';
}
mapInit(id) {
this.viewer = new Viewer(id, {
animation: false,
geocoder: false,
homeButton: false,
sceneModePicker: false,
navigationHelpButton: false,
timeline: false,
baseLayerPicker: true,
infoBox: false,
selectionIndicator: false,
fullscreenButton: false,
shouldAnimate: true
});
this.viewer.baseLayerPicker.viewModel.selectedImagery = this.viewer.baseLayerPicker.viewModel.imageryProviderViewModels[12];
this.viewer.extend(viewerCesiumNavigationMixin);
this.viewer.clock.multiplier = 200;
this.viewer.clock.shouldAnimate = true;
Camera.DEFAULT_VIEW_RECTANGLE = Rectangle.fromDegrees(89.5, 20.4, 110.4, 61.2);
const eventHandler = new ScreenSpaceEventHandler(this.viewer.scene.canvas);
eventHandler.setInputAction((movement) => {
const cartesian = this.viewer.camera.pickEllipsoid(movement.endPosition, this.viewer.scene.globe.ellipsoid);
if (cartesian) {
const cartographic = this.viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
const longitude = Math.toDegrees(cartographic.longitude).toFixed(6);
const latitude = Math.toDegrees(cartographic.latitude).toFixed(6);
const altitude = this.viewer.scene.globe.getHeight(cartographic);
const strAltitude = altitude ? altitude.toFixed(2) : '0';
const height = this.viewer.camera.positionCartographic.height.toFixed(2);
this.positionDetail.longitude = longitude + '\u00B0';
this.positionDetail.latitude = latitude + '\u00B0';
this.positionDetail.altitude = strAltitude + '米';
this.positionDetail.height = height + '米';
}
}, ScreenSpaceEventType.MOUSE_MOVE);
}
这样一个大致的样式就能搭建起来,后边就能继续深入学习cesium的各种知识了。
|