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

相关文章

从原理到实战解析Java Stream 的并行流性能优化

《从原理到实战解析JavaStream的并行流性能优化》本文给大家介绍JavaStream的并行流性能优化:从原理到实战的全攻略,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的... 目录一、并行流的核心原理与适用场景二、性能优化的核心策略1. 合理设置并行度:打破默认阈值2. 避免装箱

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

Java实现复杂查询优化的7个技巧小结

《Java实现复杂查询优化的7个技巧小结》在Java项目中,复杂查询是开发者面临的“硬骨头”,本文将通过7个实战技巧,结合代码示例和性能对比,手把手教你如何让复杂查询变得优雅,大家可以根据需求进行选择... 目录一、复杂查询的痛点:为何你的代码“又臭又长”1.1冗余变量与中间状态1.2重复查询与性能陷阱1.

Python内存优化的实战技巧分享

《Python内存优化的实战技巧分享》Python作为一门解释型语言,虽然在开发效率上有着显著优势,但在执行效率方面往往被诟病,然而,通过合理的内存优化策略,我们可以让Python程序的运行速度提升3... 目录前言python内存管理机制引用计数机制垃圾回收机制内存泄漏的常见原因1. 循环引用2. 全局变

Python多线程应用中的卡死问题优化方案指南

《Python多线程应用中的卡死问题优化方案指南》在利用Python语言开发某查询软件时,遇到了点击搜索按钮后软件卡死的问题,本文将简单分析一下出现的原因以及对应的优化方案,希望对大家有所帮助... 目录问题描述优化方案1. 网络请求优化2. 多线程架构优化3. 全局异常处理4. 配置管理优化优化效果1.

MySQL中优化CPU使用的详细指南

《MySQL中优化CPU使用的详细指南》优化MySQL的CPU使用可以显著提高数据库的性能和响应时间,本文为大家整理了一些优化CPU使用的方法,大家可以根据需要进行选择... 目录一、优化查询和索引1.1 优化查询语句1.2 创建和优化索引1.3 避免全表扫描二、调整mysql配置参数2.1 调整线程数2.

深入解析Java NIO在高并发场景下的性能优化实践指南

《深入解析JavaNIO在高并发场景下的性能优化实践指南》随着互联网业务不断演进,对高并发、低延时网络服务的需求日益增长,本文将深入解析JavaNIO在高并发场景下的性能优化方法,希望对大家有所帮助... 目录简介一、技术背景与应用场景二、核心原理深入分析2.1 Selector多路复用2.2 Buffer

SpringBoot利用树形结构优化查询速度

《SpringBoot利用树形结构优化查询速度》这篇文章主要为大家详细介绍了SpringBoot利用树形结构优化查询速度,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一个真实的性能灾难传统方案为什么这么慢N+1查询灾难性能测试数据对比核心解决方案:一次查询 + O(n)算法解决

小白也能轻松上手! 路由器设置优化指南

《小白也能轻松上手!路由器设置优化指南》在日常生活中,我们常常会遇到WiFi网速慢的问题,这主要受到三个方面的影响,首要原因是WiFi产品的配置优化不合理,其次是硬件性能的不足,以及宽带线路本身的质... 在数字化时代,网络已成为生活必需品,追剧、游戏、办公、学习都离不开稳定高速的网络。但很多人面对新路由器

MySQL深分页进行性能优化的常见方法

《MySQL深分页进行性能优化的常见方法》在Web应用中,分页查询是数据库操作中的常见需求,然而,在面对大型数据集时,深分页(deeppagination)却成为了性能优化的一个挑战,在本文中,我们将... 目录引言:深分页,真的只是“翻页慢”那么简单吗?一、背景介绍二、深分页的性能问题三、业务场景分析四、