cesium很全的入门教程 |
您所在的位置:网站首页 › 配件的概念 › cesium很全的入门教程 |
Cesium WorkShop-cesium快速入门教程
快速入门教程基本涵盖了大多数的CesiumJS API概念,主要用于Cesium基本入门,对Cesium有全面基本的了解和使用 一、概述本教程会一步一步教会你做一个项目,主要介绍如下内容: 配置1个Cesium viewer加载各种数据集创建几何和设置样式加载3D Tiles控制相机添加鼠标交互学完后你将会创建如下项目Viewer是一个综合,包含中心的地球globe,周围的控件widget,如果只需要一个地球的话,使用可以cesiumwidget()来初始化,viewer的内部源码就是使用了cesiumwidget()创建地球,并创建一些div来生成widget控件 Cesium的数据服务器,功能很强大,但是国内一般不用 4.1 添加影像地图Adding Imagery支持的 Imagery Formats: WMS TMS WMTS (with time dynamic imagery) ArcGIS Bing Maps Google Earth Mapbox Open Street Map Cesium提供了不同的接口加载上述几种类型的服务,但都是使用ImageryProvider来加载,可通过该链接查看所有接口类型many different providers 示例如下: // 去掉默认底图bing Remove default base layer viewer.imageryLayers.remove(viewer.imageryLayers.get(0)); // Add Sentinel-2 imagery viewer.imageryLayers.addImageryProvider(new Cesium.IonImageryProvider({ assetId : 3954 }));通过以上示例可以知道,默认的Cesium会加载一个底图,是bingMap底图,网络不太好,一般要先去掉这个默认的,加载我们自己的 五、添加地形服务Adding Terrain示例代码如下: // Load Cesium World Terrain viewer.terrainProvider = Cesium.createWorldTerrain({ requestWaterMask : true, // required for water effects requestVertexNormals : true // required for terrain lighting }); // Enable depth testing so things behind the terrain disappear. viewer.scene.globe.depthTestAgainstTerrain = true;requestVertexNormals是地形光照效果,需要打开场景光源支持,depthTestAgainstTerrain是地形遮挡效果开关,打开后地形会遮挡看不到的区域 六、配置Scene重点标注:影像和地形都是加载scene中的globe对象上的,但是其他的entiy和3dtiles等,是加载在scene上的,和globe是同级别的,关闭globe,entiy等也会显示 6.1 场景中的坐标概念简介:Cartesian3 : 世界坐标系,坐标原点在globe球心的一个坐标系,坐标值是以米为单位,所以Cartesian3的坐标值大多是地球的半径那么大的数值。a 3D Cartesian coordinate – when used as a position it is relative to the center of the globe in meters using the Earth fixed-frame (ECEF) Cartographic : WGS84-就是经纬度坐标系,地球的经纬度值,单位是度,如118°。a position defined by longitude, latitude (in radians) and height from the WGS84 ellipsoid surface 局部坐标系:啥叫局部坐标系呢?很简单,就是你自己在地球上找个经纬度点,把他当原点,形成一个坐标系。 看代码: // Get the transform from local east-north-up at cartographic (0.0, 0.0) to Earth's fixed frame. var center = Cesium.Cartesian3.fromDegrees(118.0, 30.0); var transform = Cesium.Transforms.eastNorthUpToFixedFrame(center);我们把坐标原点移动到了118.0, 30.0这个地方,x轴指向东,y轴指向北,还有northEastDownToFixedFrame等不同的方向坐标轴 HeadingPitchRoll : HPR地球视角上下左右偏移调整,参考博客相机中的HeadingPitchRoll。 Quaternion : 行话为四元数,其实就是使一个物体围绕指定的轴旋转多少角度。和旋转方法,旋转矩阵是同步改变的。(A 3D rotation represented as 4D coordinates.) 6.2 相机控制(Camera Control)viewer.scene.Camera ,就是单纯的实现视角的各种移动缩放等 Camera.setView(options) : 直接定位到一个视角,没动画set camera at specific position and orientation immediatelyCamera.zoomIn(amount) : 缩放多少米move camera forward along the view vectorCamera.zoomOut(amount) : 缩放多少米move camera backwards along the view vectorCamera.flyTo(options) : 最常用,有动画过程create an animated camera flight from the current camera position to a new positionCamera.lookAt(target, offset) : 定位到一个点,并以这个点为中心锁定orient and position the camera to look at target point with given offsetCamera.move(direction, amount) : 平移一定距离move the camera in any directionCamera.rotate(axis, angle) : 旋转rotate the camera about any axis 示例: // Create an initial camera view var initialPosition = new Cesium.Cartesian3.fromDegrees(-73.998114468289017509, 40.674512895646692812, 2631.082799425431); var initialOrientation = new Cesium.HeadingPitchRoll.fromDegrees(7.1077496389876024807, -31.987223091598949054, 0.025883251314954971306); var homeCameraView = { destination : initialPosition, orientation : { heading : initialOrientation.heading, pitch : initialOrientation.pitch, roll : initialOrientation.roll } }; // Set the initial view viewer.scene.camera.setView(homeCameraView);可以修改cesium默认的主页按钮定位位置 // Add some camera flight animation options homeCameraView.duration = 2.0;//飞行用时 homeCameraView.maximumHeight = 2000;//飞行过程中最大高度 homeCameraView.pitchAdjustHeight = 2000; homeCameraView.endTransform = Cesium.Matrix4.IDENTITY; // Override the default home button覆盖home默认事件 viewer.homeButton.viewModel.command.beforeExecute.addEventListener(function (e) { e.cancel = true; viewer.scene.camera.flyTo(homeCameraView); });坐标系转换:参考 6.3 Clock Control这个在沿着路线飞行漫游的时候能用到,暂时不会,后期补充,举个使用列子吧在这 // Set up clock and timeline. viewer.clock.shouldAnimate = true; // make the animation play when the viewer starts viewer.clock.startTime = Cesium.JulianDate.fromIso8601("2017-07-11T16:00:00Z"); viewer.clock.stopTime = Cesium.JulianDate.fromIso8601("2017-07-11T16:20:00Z"); viewer.clock.currentTime = Cesium.JulianDate.fromIso8601("2017-07-11T16:00:00Z"); viewer.clock.multiplier = 2; // sets a speedup viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER; // tick computation mode viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP; // loop at the end viewer.timeline.zoomTo(viewer.clock.startTime, viewer.clock.stopTime); // set visible range 七、加载EntitiesEntities相较于Primitive接口更加简单直观,是对Primitive封装 常见的Entities: PolygonPolylineBillboardLabel 可以使用viewer.entities.add()方法直接添加entity到一个默认的dataSource里面,DataSource是添加各种entity的集合,默认有一个defaultDataSource,viewer.entities.add()如果不知道加载到哪个DataSource里面,就会被默认加载到defaultDataSource里。 DataSource有一下几类:CustomDataSource-就是最常见的添加Entity类型的 var dataSource = new Cesium.CustomDataSource('myData'); var entity = dataSource.entities.add({ position : Cesium.Cartesian3.fromDegrees(1, 2, 0), billboard : { image : 'image.png' } }); viewer.dataSources.add(dataSource); CzmlDataSource-加载czml文件格式的数据GeoJsonDataSource-加载geojson格式的数据 var viewer = new Cesium.Viewer('cesiumContainer'); viewer.dataSources.add(Cesium.GeoJsonDataSource.load('../../SampleData/ne_10m_us_states.topojson', { stroke: Cesium.Color.HOTPINK, fill: Cesium.Color.PINK, strokeWidth: 3, markerSymbol: '?' })); KmlDataSource-加载kml格式的数据 var kmlOptions = { camera : viewer.scene.camera, canvas : viewer.scene.canvas, clampToGround : true }; // Load geocache points of interest from a KML file // Data from : http://catalog.opendata.city/dataset/pediacities-nyc-neighborhoods/resource/91778048-3c58-449c-a3f9-365ed203e914 var geocachePromise = Cesium.KmlDataSource.load('./Source/SampleData/sampleGeocacheLocations.kml', kmlOptions); // Add geocache billboard entities to scene and style them geocachePromise.then(function(dataSource) { // Add the new data as entities to the viewer viewer.dataSources.add(dataSource); // Get the array of entities var geocacheEntities = dataSource.entities.values; for (var i = 0; i |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |