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

相关文章

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

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

MySQL中慢SQL优化的不同方式介绍

《MySQL中慢SQL优化的不同方式介绍》慢SQL的优化,主要从两个方面考虑,SQL语句本身的优化,以及数据库设计的优化,下面小编就来给大家介绍一下有哪些方式可以优化慢SQL吧... 目录避免不必要的列分页优化索引优化JOIN 的优化排序优化UNION 优化慢 SQL 的优化,主要从两个方面考虑,SQL 语

MySQL中慢SQL优化方法的完整指南

《MySQL中慢SQL优化方法的完整指南》当数据库响应时间超过500ms时,系统将面临三大灾难链式反应,所以本文将为大家介绍一下MySQL中慢SQL优化的常用方法,有需要的小伙伴可以了解下... 目录一、慢SQL的致命影响二、精准定位问题SQL1. 启用慢查询日志2. 诊断黄金三件套三、六大核心优化方案方案

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、

使用国内镜像源优化pip install下载的方法步骤

《使用国内镜像源优化pipinstall下载的方法步骤》在Python开发中,pip是一个不可或缺的工具,用于安装和管理Python包,然而,由于默认的PyPI服务器位于国外,国内用户在安装依赖时可... 目录引言1. 为什么需要国内镜像源?2. 常用的国内镜像源3. 临时使用国内镜像源4. 永久配置国内镜