// cesium 地图效果整合
/**
* 在Cesium中更换地图
* @param {Viewer} viewer - Cesium的Viewer实例
* @param {String} type - 地图类型,可选值为 'baidu'、'guge'、'tengxun'、'tianditu'、'gaode'
* @param {Object} options - 针对不同地图类型的配置选项
*/
function replacementMap(viewer, type, options) {
if (type == 'baidu') {
viewer.imageryLayers.removeAll();
viewer.imageryLayers.add(
new Cesium.ImageryLayer(new AmapImageryProvider(options))
);
} else if (type == 'guge') {
viewer.imageryLayers.removeAll();
viewer.imageryLayers.add(
new Cesium.ImageryLayer(new GoogleImageryProvider(options))
);
} else if (type == 'tengxun') {
viewer.imageryLayers.removeAll();
viewer.imageryLayers.add(
new Cesium.ImageryLayer(new TencentImageryProvider(options))
);
} else if (type == 'tianditu') {
viewer.imageryLayers.removeAll();
viewer.imageryLayers.add(
new Cesium.ImageryLayer(new TdtImageryProvider(options))
);
} else if (type == 'gaode') {
viewer.imageryLayers.removeAll();
viewer.imageryLayers.add(
new Cesium.ImageryLayer(new AmapImageryProvider(options))
);
}
}
/**
* 在Cesium中加载离线地图
* @param {Viewer} viewer - Cesium的Viewer实例
* @param {String} url - 离线地图的URL,可以是本地文件路径或远程URL
*/
function offlineMap(viewer, url) {
viewer.imageryLayers.removeAll();
viewer.imageryLayers.addImageryProvider(
new Cesium.UrlTemplateImageryProvider({
url: url,
})
);
}
/**
* 创建简单的 Cesium 地图
* @param {Object} that - 上下文对象,通常是指调用该函数的对象
* @param {String} mapName - 地图容器的名称或ID
*/
function simpleMap(that, mapName) {
Cesium.Ion.defaultAccessToken =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJlYTQ2ZjdjNS1jM2E0LTQ1M2EtOWM0My1mODMzNzY3YjYzY2YiLCJpZCI6MjkzMjcsInNjb3BlcyI6WyJhc3IiLCJnYyJdLCJpYXQiOjE1OTE5NDIzNjB9.RzKlVTVDTQ9r7cqCo-PDydgUh8Frgw0Erul_BVxiS9c";
that.viewer = new Cesium.Viewer(mapName, {
shouldAnimate: true,
animation: false, // 是否显示动画控件
baseLayerPicker: false, // 是否显示图层选择控件
vrButton: false, // 是否显示VR控件
geocoder: false, // 是否显示地名查找控件
timeline: false, // 是否显示时间线控件
sceneModePicker: false, // 是否显示投影方式控件
navigationHelpButton: false, // 是否显示帮助信息控件
navigationInstructionsInitiallyVisible: false, // 帮助按钮,初始化的时候是否展开
infoBox: false, // 是否显示点击要素之后显示的信息
fullscreenButton: false, // 是否显示全屏按钮
selectionIndicator: true, // 是否显示选中指示框
homeButton: false, // 是否显示返回主视角控件
scene3DOnly: false, // 如果设置为true,则所有几何图形以3D模式绘制以节约GPU资源})
});
that.viewer._cesiumWidget._creditContainer.style.display = "none";
}
// cesium 3D/2D切换
/**
* @description: cesium 3D/2D切换
* @param {Viewer} viewer - Cesium的Viewer实例
*/
function TwoD(viewer) {
viewer.scene.morphTo2D(0);
}
function ThereD(viewer) {
var scene = viewer.scene;
scene.morphTo3D(0);
}
// cesium 地球自转方法
/**
* @description: cesium 地球自转方法
* @param {any} viewer - Cesium的Viewer实例
*/
function earthRotation(viewer) {
viewer.clock.multiplier = 100;//背景转速值越大越快
viewer.clock.shouldAnimate = true;
let previousTime = viewer.clock.currentTime.secondsOfDay;
function onTickCallback() {
var spinRate = 1;
var currentTime = viewer.clock.currentTime.secondsOfDay;
var delta = (currentTime - previousTime) / 5000;// 5000是地球转速,值越小转越快
previousTime = currentTime;
viewer.scene.camera.rotate(Cesium.Cartesian3.UNIT_Z, -spinRate * delta);
}
//监听点击事件(不然点击到Titus上他不停止旋转)
var handlerEarthRotation = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
handlerEarthRotation.setInputAction(function (click) {
viewer.clock.shouldAnimate = false;
viewer.clock.onTick.removeEventListener(onTickCallback);
handlerEarthRotation.destroy();
}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
if (obj.rotation == true) {
viewer.clock.onTick.addEventListener(onTickCallback);
} else {
viewer.clock.shouldAnimate = false;
viewer.clock.onTick.removeEventListener(onTickCallback);
}
}
/**
* @description: cesium 背景修改成其它颜色
* @param {any} viewer - Cesium的Viewer实例
*/
function Skybox(viewer) {
viewer.scene.skyBox.show = false;
// cesium 背景颜色修改完成
viewer.scene.backgroundColor = Cesium.Color.GREEN;
}
/**
* @description: 背景修改成天空盒(根据时间不同天空盒背景不同)
* @param {any} viewer - Cesium的Viewer实例
*/
function sky(viewer) {
//大气
viewer.scene.skyAtmosphere.show = false;
viewer.scene.globe.enableLighting = false; // 关闭灯光
// 获取当前时间并判断是属于哪个时间段
const now = new Date();
const hour = now.getHours();
if (hour >= 4 && hour < 8) {
// 亮度设置
var stages = viewer.scene.postProcessStages;
viewer.scene.brightness = viewer.scene.brightness || stages.add(Cesium.PostProcessStageLibrary.createBrightnessStage());
viewer.scene.brightness.enabled = true;
viewer.scene.brightness.uniforms.brightness = Number(0.8);
viewer.scene.skyBox = new Cesium.SkyBox({
sources: {
positiveX: '/images/qingchen/px.png',
negativeX: '/images/qingchen/nx.png',
positiveY: '/images/qingchen/ny.png',
negativeY: '/images/qingchen/py.png',
positiveZ: '/images/qingchen/pz.png',
negativeZ: '/images/qingchen/nz.png',
}
})
} else if (hour >= 8 && hour < 17) {
// 亮度设置
var stages = viewer.scene.postProcessStages;
viewer.scene.brightness = viewer.scene.brightness || stages.add(Cesium.PostProcessStageLibrary.createBrightnessStage());
viewer.scene.brightness.enabled = true;
viewer.scene.brightness.uniforms.brightness = Number(1.2);
viewer.scene.skyBox = new Cesium.SkyBox({
sources: {
positiveX: '/images/zhengwu/px.png',
negativeX: '/images/zhengwu/nx.png',
positiveY: '/images/zhengwu/ny.png',
negativeY: '/images/zhengwu/py.png',
negativeZ: '/images/zhengwu/nz.png',
positiveZ: '/images/zhengwu/pz.png',
}
})
} else if (hour >= 17 && hour < 19) {
// 亮度设置
var stages = viewer.scene.postProcessStages;
viewer.scene.brightness = viewer.scene.brightness || stages.add(Cesium.PostProcessStageLibrary.createBrightnessStage());
viewer.scene.brightness.enabled = true;
viewer.scene.brightness.uniforms.brightness = Number(1);
viewer.scene.skyBox = new Cesium.SkyBox({
sources: {
positiveX: '/images/huanghun/px.png',
negativeX: '/images/huanghun/nx.png',
positiveY: '/images/huanghun/ny.png',
negativeY: '/images/huanghun/py.png',
negativeZ: '/images/huanghun/nz.png',
positiveZ: '/images/huanghun/pz.png',
}
})
} else if (hour >= 19 && hour < 25) {
// cesium 隐藏太阳
viewer.scene.globe.enableLighting = false;
viewer.shadows = false;
viewer.scene.sun.show = false;
// 亮度设置
var stages = viewer.scene.postProcessStages;
viewer.scene.brightness = viewer.scene.brightness || stages.add(Cesium.PostProcessStageLibrary.createBrightnessStage());
viewer.scene.brightness.enabled = true;
viewer.scene.brightness.uniforms.brightness = Number(0.4);
viewer.scene.skyBox = new Cesium.SkyBox({
sources: {
positiveX: '/images/heiye/px.png',
negativeX: '/images/heiye/nx.png',
positiveY: '/images/heiye/ny.png',
negativeY: '/images/heiye/py.png',
negativeZ: '/images/heiye/nz.png',
positiveZ: '/images/heiye/pz.png',
}
})
} else if (hour >= 1 && hour < 4) {
// cesium 隐藏太阳
viewer.scene.globe.enableLighting = false;
viewer.shadows = false;
viewer.scene.sun.show = false;
// 亮度设置
var stages = viewer.scene.postProcessStages;
viewer.scene.brightness = viewer.scene.brightness || stages.add(Cesium.PostProcessStageLibrary.createBrightnessStage());
viewer.scene.brightness.enabled = true;
viewer.scene.brightness.uniforms.brightness = Number(0.4);
viewer.scene.skyBox = new Cesium.SkyBox({
sources: {
positiveX: '/images/heiye/px.png',
negativeX: '/images/heiye/nx.png',
positiveY: '/images/heiye/ny.png',
negativeY: '/images/heiye/py.png',
negativeZ: '/images/heiye/nz.png',
positiveZ: '/images/heiye/pz.png',
}
})
}
}
/**
* @description: 使用3D地形
* @param {any} viewer - Cesium的Viewer实例
*/
function thereDmap(viewer) {
// 调用在线地形图使用恒歌科技在线地形图
const terrainProvider = new Cesium.CesiumTerrainProvider({
url: "http://www.freexgis.com/web-data/terrain",
});
viewer.terrainProvider = terrainProvider;
let inter = setInterval(() => {
// 改变时间设置光照效果
let date = new Date().getTime()
let utc = Cesium.JulianDate.fromDate(new Date(date))
//北京时间
viewer.clockViewModel.currentTime = Cesium.JulianDate.addHours(utc, 0, new Cesium.JulianDate())
}, 100)
}
/**
* @description: 3D地形使用方法
* @param {any} viewer - Cesium的Viewer实例
*/
function thereMap(viewer) {
var terrainProvider = new Cesium.ArcGISTiledElevationTerrainProvider({
url:
"https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer",
token:
"KED1aF_I4UzXOHy3BnhwyBHU4l5oY6rO6walkmHoYqGp4XyIWUd5YZUC1ZrLAzvV40pR6gBXQayh0eFA8m6vPg..",
});
viewer.terrainProvider = terrainProvider;
viewer.scene.terrainExaggeration = 3;
}
/**
* @description: 添加大气层(静止的大气层)
* @param {any} viewer - Cesium的Viewer实例
*/
function atmosphere(viewer) {
const entities = viewer.entities;
const image = 'https://openlayers.vip/examples/resources/earth_cloud.png';
const imageMaterial = new Cesium.ImageMaterialProperty({
image: image
});
const entity = entities.add({
rectangle: {
coordinates: Cesium.Rectangle.fromDegrees(-180.0, -90.0, 180.0, 90.0),
material: imageMaterial,
},
});
kaiqi()
function kaiqi() {
entity.rectangle.height = undefined;
entity.rectangle.extrudedHeight = undefined;
entity.rectangle.material = imageMaterial;
}
}
// cesium 创建太阳
/**
* @description: 创建太阳
* @param {any} viewer - Cesium的Viewer实例
*/
function makeSun(viewer) {
viewer.scene.sun.show = true;
viewer.scene.sun.glowFactor = 10; //太阳变大
}
/**
* @description: 暗影系高德地图(适合运用于黑夜形的智慧城市地图)
* @param {any} viewer - Cesium的Viewer实例
*/
function anyingMap(viewer) {
viewer.imageryLayers.removeAll(); // 移除所有现有的图像图层
// cesium 底图更换成暗系高德底图
var imageryProvider = new Cesium.ArcGisMapServerImageryProvider({
url: 'http://map.geoq.cn/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer'
});
var imageryLayer = viewer.imageryLayers.addImageryProvider(imageryProvider);
}
export {
replacementMap,
offlineMap,
simpleMap,
TwoD,
ThereD,
earthRotation,
Skybox,
sky,
thereDmap,
thereMap,
atmosphere,
makeSun,
anyingMap
};
cesium 地图效果
最新推荐文章于 2024-06-22 19:10:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)