- //加载KML流数据,生成移动路径和模型,模型按路径移动,设置第一视角,标签显示
- function LoadKMLData1() {
- //先请求出数据
- let flyArry = [],czml_hh,time = 0.0
- $.get('../Apps/SampleData/geojson/flyroad_1.json', function (data) {
- for (let i = 0; i < data.features.length; i++) {
- x = data.features[i].geometry.coordinates[0]
- y = data.features[i].geometry.coordinates[1]
- z = data.features[i].properties.Z
- time += data.features[i].properties.time *0.1
- flyArry.push(time,x, y, z)
- }
- czml_hh = [
- {
- //文本,时间
- id: 'document',
- name: '时间',
- version: '1.0',
- clock: {
- interval: '2021-03-16T10:00:00Z/2021-03-16T10:07:45Z',
- currentTime: '2021-03-16T10:00:01Z',
- multiplier: 1,
- },
- },
- {
- id: 'hhriverRoamingModel',
- name: '黄河八电站漫游飞机',
- description: '从上游电站开始往下游电站漫游飞行模型',
- availability: '2021-03-16T10:00:00Z/2021-03-16T10:07:45Z',
- //加载的模型数据
- model: {
- gltf: '../Apps/SampleData/models/CesiumAir/Cesium_Air.glb',
- scale: 5,
- },
- //旋转的方向
- orientation: {
- velocityReference: '#position',
- },
- viewFrom: {
- cartesian: [-1480, -1315, 1079],
- },
- //显示的文字信息
- label: {
- fillColor: [
- {
- interval: '2021-03-16T10:00:00Z/2021-03-16T10:07:45Z',
- rgba: [255, 255, 255, 255],
- },
- ],
- font: 'bold 14pt Segoe UI Semibold',
- horizontalOrigin: 'CENTER',
- showBackground: true,
- backgroundColor: {
- rgba: [0, 80, 142, 180],
- },
- backgroundPadding: {
- cartesian2: [10, 7],
- },
- outlineColor: {
- rgba: [0, 0, 0, 255],
- },
- pixelOffset: {
- cartesian2: [0.0, -100.0],
- },
- scale: 1.0,
- show: [
- {
- interval: '2021-03-16T10:00:00Z/2021-03-16T10:07:45Z',
- boolean: true,
- },
- ],
- style: 'FILL',
- text: '飞机漫游',
- verticalOrigin: 'CENTER',
- },
- //显示路径
- path: {
- resolution: 1,//将路径显示为以1秒为增量采样的路径线
- id: 'hhriverRoamingPath',
- name: '黄河八电站漫游路径',
- description: '从上游电站开始往下游电站漫游飞行路径',
- availability: '2021-03-16T10:00:00Z/2021-03-16T10:07:45Z',
- material: {
- 'polylineGlow': {
- 'color': {
- 'rgba': [
- 0,
- 0,
- 200,
- 255
- ]
- },
- 'glowPower': 0.4,
- }
- },
- width: 3,
- // leadTime: 10,
- // trailTime: 1000,
- // resolution: 5,
- },
- //位置关系
- position: {
- interpolationAlgorithm: 'HERMITE',
- interpolationDegree: 0.5,
- epoch: '2021-03-16T10:00:00Z',
- cartographicDegrees: flyArry,
- },
- },
- ];
- })
- function initLJMY(){
- var dataSourcePromise = viewer.dataSources.add(
- Cesium.CzmlDataSource.load(czml_hh),
- );
- dataSourcePromise
- .then((dataSource) => {
- viewer.trackedEntity = dataSource.entities.getById(
- 'hhriverRoamingModel',
- );
- //定义一个全局变量等于这个值 好做实时的显隐控制
- this.roamingEntity = viewer.trackedEntity;
- //是否显示路径
- viewer.trackedEntity.path.show = true;
- //是否显示模型
- viewer.trackedEntity.model.show = true;
- //是否开始播放动画
- viewer.clock.shouldAnimate = true;
- //第一视角
- firstPersonModel();
- //定义时间参数
- this.startTime = viewer.clock.currentTime.clone();
- }).otherwise((error) => {
- alert(error);
- });
- }
- //设置第一视角
- function firstPersonModel() {
- if(viewer.scene){
- viewer.scene.preRender.addEventListener(setCameraLookAt);
- }
- }
- //设置摄像机事件
- function setCameraLookAt() {
- if (viewer.trackedEntity) {
- let center = viewer.trackedEntity.position.getValue(
- viewer.clock.currentTime
- );
- let orientation = viewer.trackedEntity.orientation.getValue(
- viewer.clock.currentTime
- );
- // 圆滑路径,使路径相对平滑过渡 插值过渡
- viewer.trackedEntity.position.setInterpolationOptions({
- interpolationAlgorithm: Cesium.HermitePolynomialApproximation, //插值算法
- interpolationDegree: 2
- });
- //实时计算飞行时模型的航向角度
- let transform = Cesium.Matrix4.fromRotationTranslation(Cesium.Matrix3.fromQuaternion(orientation), center);
- viewer && viewer.camera.lookAtTransform(transform, new Cesium.Cartesian3(-800, 0, 300));
- }
- }
- //清除事件
- function clearReuslt() {
- try {
- viewer.clock.shouldAnimate = false;
- viewer.trackedEntity = undefined;
- viewer.dataSources.removeAll();
- } catch (err) {
- console.log('45555555555', err);
- }
- }
- //绑定事件
- document.getElementById("fly").οnclick=initLJMY;
- document.getElementById("clearReuslt").οnclick=clearReuslt;
- }
- LoadKMLData1();
Cesium基础知识-加载KML数据生成模型,路径
最新推荐文章于 2024-09-11 08:47:34 发布