cesium-二三维联动优化(ol-cesium)

2023-10-29 13:20
文章标签 优化 联动 三维 cesium ol

本文主要是介绍cesium-二三维联动优化(ol-cesium),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

cesium-二三维联动优化(ol-cesium)

之前已经实现了ol和cesium联动的效果了,但还是有点问题:cesium和ol平面视角联动还算正常的,但是当cesium为三维视角时联动的效果就出现问题。

这里使用ol-cesium来实现完善的联动效果

ol-cesium

介绍

在 2D 和 3D 之间平滑切换并同步:

  • 地图上下文(边界框和缩放级别);
  • 栅格数据源;
  • 2D 和 3D 的矢量数据源;
  • 地图选择(选定项目);
  • 地图和地球视图之间的动画过渡。

ol-cesium官网地址

安装

我这里使用的是vue-cli框架

使用npm安装ol-cesium

npm i ol-cesium

使用例子如下:

import OLCesium from 'ol-cesium';
const ol3d = new OLCesium({map: ol2dMap}); // ol2dMap is the ol.Map instance
ol3d.setEnabled(true);

具体的参考demo可以去官网看看

实现效果

ol-cesium二三维联动

我的这个项目并不需要全部的ol-cesium功能,我只需要其中一部分功能:cesium和ol视角联动的效果

最终效果实现的二三维同步效果如下:

这里我对代码进行来修改,提取了部分功能出来

实现代码

项目结构

image-20220117112614825

lib目录中为从ol-cesium中提取出来的代码

核心代码是Camera.js,其它代码为相关依赖,主要就是修改一下相应代码的依赖

主要代码

testOLCs()

使用camera方法

  • new olcsCamera(this._viewer.scene, this.map);这里传入cesium的viewer和ol的map,让camera来管理两个地图的视角
  • checkCameraChange();开启viewer和map变化的监听
render_()

用于渲染铯场景

requestAnimationFrame()

请求动画帧回调方法

核心方法
    testOlCs() {this.camera_ = new olcsCamera(this._viewer.scene, this.map);this.camera_.checkCameraChange();},/*** 渲染铯场景*/render_() {// 如果对 `requestAnimationFrame`(请求动画帧) 的调用处于挂起状态,请取消它if (this.renderId_ !== undefined) {cancelAnimationFrame(this.renderId_);this.renderId_ = undefined;}this.renderId_ = requestAnimationFrame(this.onAnimationFrame_.bind(this));},/*** Callback for `requestAnimationFrame`. 请求动画帧回调方法* @param {number} frameTime The frame time, from `performance.now()`.帧时间* @private*/onAnimationFrame_(frameTime) {this.renderId_ = undefined;// 检查帧是否在目标帧速率内渲染const interval = 1000.0 / this.targetFrameRate_;const delta = frameTime - this.lastFrameTime_;if (delta < interval) {// 太早了,还没渲染this.render_();return;}// 渲染一帧的时间,节省时间this.lastFrameTime_ = frameTime;const julianDate = this.time_();this.scene_.initializeFrame();this.scene_.render(julianDate);this.camera_.checkCameraChange();// 在这个完成后请求下一个渲染调用,以确保浏览器不会得到备份this.render_();}

完整代码

<template><div class="home"><cesiumComponent ref="refCesium"/><div id="eye"></div></div>
</template><script>
import cesiumComponent from '../cesium/cesium.vue'
import olcsCamera from './lib/Camera.js';export default {name: "olCesium01",data() {return {_viewer: undefined,scene_: undefined,view_: undefined,camera_: null,targetFrameRate_: Number.POSITIVE_INFINITY,lastFrameTime_: 0,time_: function () {return Cesium.JulianDate.now()},layer: {tiandituVecLayer: '',tiandituCvaLayer: '',tiandituImgLayer: '',tiandituCiaLayer: '',},map: '',};},components: {cesiumComponent},mounted() {this.init();this.addTiles();},methods: {init() {let that=this;this.$refs.refCesium.initMap();let viewer = this.$refs.refCesium._viewer;this._viewer = viewer;that.scene_ = viewer.scene;//渲染铯场景that.render_();this.addOlMap();this.testOlCs()},addOlMap() {var that = this;//普通地图this.layer.tiandituVecLayer = new ol.layer.Tile({title: 'generalMap',source: new ol.source.XYZ({url: 'http://t3.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=token',crossOrigin: 'anonymous'}),// zIndex: 1,visible: true});//普通地图标记that.layer.tiandituCvaLayer = new ol.layer.Tile({title: 'generalMapZj',source: new ol.source.XYZ({url: 'http://t3.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=token',crossOrigin: 'anonymous'}),visible: true});//影像地图that.layer.tiandituImgLayer = new ol.layer.Tile({title: 'generalMapImg',source: new ol.source.XYZ({url: 'http://t3.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=token',crossOrigin: 'anonymous'}),visible: true});//影像地图标注that.layer.tiandituCiaLayer = new ol.layer.Tile({title: 'generalMapImgZj',source: new ol.source.XYZ({url: 'http://t3.tianditu.com/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=token',crossOrigin: 'anonymous'}),visible: true});this.map = new ol.Map({target: 'eye',layers: [that.layer.tiandituImgLayer,that.layer.tiandituCiaLayer,that.layer.tiandituVecLayer,that.layer.tiandituCvaLayer,],view: new ol.View({center: [13410926.774433982, 3715530.4937355495],zoom: 12}),controls: ol.control.defaults({attributionOptions: {collapsible: false}})});this.view_ = this.map.getView();},addTiles() {// cesium加载代码let tileSet = this._viewer.scene.primitives.add(new Cesium.Cesium3DTileset({url: '../res/data/3dtiles/tianjie/tileset.json',modelMatrix: Cesium.Matrix4.fromArray([0.9972458032561666, 0.04372029028528979, 0.05991113506964879, 0,-0.03623787897545647, 0.9920229449104262, -0.12073646051879428, 0,-0.06471185374661931, 0.11823287609043515, 0.9908750491338749, 0,-663.0794944260269, 1211.490494620055, 2974.1003134818748, 1]),}));this._viewer.flyTo(tileSet);},testOlCs() {this.camera_ = new olcsCamera(this._viewer.scene, this.map);this.camera_.checkCameraChange();},/*** Render the Cesium scene*/render_() {// if a call to `requestAnimationFrame` is pending, cancel itif (this.renderId_ !== undefined) {cancelAnimationFrame(this.renderId_);this.renderId_ = undefined;}this.renderId_ = requestAnimationFrame(this.onAnimationFrame_.bind(this));},/*** Callback for `requestAnimationFrame`.* @param {number} frameTime The frame time, from `performance.now()`.* @private*/onAnimationFrame_(frameTime) {this.renderId_ = undefined;// check if a frame was rendered within the target frame rateconst interval = 1000.0 / this.targetFrameRate_;const delta = frameTime - this.lastFrameTime_;if (delta < interval) {// too soon, don't render yetthis.render_();return;}// time to render a frame, save the timethis.lastFrameTime_ = frameTime;const julianDate = this.time_();this.scene_.initializeFrame();this.scene_.render(julianDate);this.camera_.checkCameraChange();// request the next render call after this one completes to ensure the browser doesn't get backed upthis.render_();}},created() {},
}
</script><style scoped>
.home {height: 100%;width: 100%;margin: 0;padding: 0;overflow: hidden;
}#eye {position: absolute;width: 20%;height: 20%;bottom: 0;right: 0;z-index: 999;background: red;border: solid blue 1px;
}#cesiumContainer {height: 100%;width: 100%;margin: 0;padding: 0;overflow: hidden;
}
</style>

image-20220117135301545

这篇关于cesium-二三维联动优化(ol-cesium)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/300908

相关文章

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

SpringBoot中HTTP连接池的配置与优化

《SpringBoot中HTTP连接池的配置与优化》这篇文章主要为大家详细介绍了SpringBoot中HTTP连接池的配置与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、HTTP连接池的核心价值二、Spring Boot集成方案方案1:Apache HttpCl

PyTorch高级特性与性能优化方式

《PyTorch高级特性与性能优化方式》:本文主要介绍PyTorch高级特性与性能优化方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、自动化机制1.自动微分机制2.动态计算图二、性能优化1.内存管理2.GPU加速3.多GPU训练三、分布式训练1.分布式数据

MySQL中like模糊查询的优化方案

《MySQL中like模糊查询的优化方案》在MySQL中,like模糊查询是一种常用的查询方式,但在某些情况下可能会导致性能问题,本文将介绍八种优化MySQL中like模糊查询的方法,需要的朋友可以参... 目录1. 避免以通配符开头的查询2. 使用全文索引(Full-text Index)3. 使用前缀索

C#实现高性能Excel百万数据导出优化实战指南

《C#实现高性能Excel百万数据导出优化实战指南》在日常工作中,Excel数据导出是一个常见的需求,然而,当数据量较大时,性能和内存问题往往会成为限制导出效率的瓶颈,下面我们看看C#如何结合EPPl... 目录一、技术方案核心对比二、各方案选型建议三、性能对比数据四、核心代码实现1. MiniExcel

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis