有些业务需求,不能访问在线地图,这时需要我们将互联网地图下载到本地,以离线地图的方式,供业务使用。
1. 下载地图影像
在QGIS使用专栏里,已经说明了下载影像的方法,可以点击查看使用QGIS下载影像并切片-CSDN博客。
2. 完整示例代码
MapWorks.js
// 初始视图定位在中国
Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(90, -20, 110, 90);
let viewer = null;
function initMap(container) {
viewer = new Cesium.Viewer(container, {
animation: false,
baseLayerPicker: false,
fullscreenButton: false,
geocoder: false,
homeButton: false,
infoBox: false,
sceneModePicker: false,
selectionIndicator: false,
timeline: false,
navigationHelpButton: false,
scene3DOnly: true,
orderIndependentTranslucency: false,
contextOptions: {
webgl: {
alpha: true
}
}
})
viewer._cesiumWidget._creditContainer.style.display = 'none'
viewer.scene.fxaa = true
viewer.scene.postProcessStages.fxaa.enabled = true
if (Cesium.FeatureDetection.supportsImageRenderingPixelated()) {
// 判断是否支持图像渲染像素化处理
viewer.resolutionScale = window.devicePixelRatio
}
// 移除默认影像
removeAll()
// 地形深度测试
viewer.scene.globe.depthTestAgainstTerrain = true
// 背景色
// viewer.scene.globe.backgroundColor = new Cesium.Color(0, 0, 0, 0)
viewer.scene.globe.baseColor = new Cesium.Color(0.0, 0.0, 0.0, 0)
// viewer.scene.backgroundColor = new Cesium.Color(0, 0, 0, 0)
}
function loadOfflineMap() {
const url = "http://47.107.93.79:8600/images/yx/{z}/{x}/{y}.png"
const layerProvider = new Cesium.UrlTemplateImageryProvider({
url: url
})
viewer.imageryLayers.addImageryProvider(layerProvider);
let coords = [103.92087436455071, 30.622945990712786, 15000]
let hpr = {
heading: 0,
pitch:-90.0,
roll: 0
}
setView(coords, hpr)
}
function removeAll() {
viewer.imageryLayers.removeAll();
}
function destroy() {
viewer.entities.removeAll();
viewer.imageryLayers.removeAll();
viewer.destroy();
}
function setView(coords,hpr) {
viewer.scene.camera.flyTo({
destination: Cesium.Cartesian3.fromDegrees(coords[0], coords[1], coords[2]),
orientation: {
heading: Cesium.Math.toRadians(hpr.heading),
pitch: Cesium.Math.toRadians(hpr.pitch),
roll: Cesium.Math.toRadians(hpr.roll),
}
});
}
export {
initMap,
loadOfflineMap,
setView,
removeAll,
destroy
}
OfflineLayer.vue
<!--
* @Description:
* @Author: maizi
* @Date: 2023-04-03 11:34:28
* @LastEditTime: 2023-04-04 19:10:25
* @LastEditors: maizi
-->
<template>
<div id="container">
<div class="pane_container">
<el-button size="small" @click="loadOfflineMap()">离线地图</el-button>
</div>
</div>
</template>
<script>
import * as MapWorks from './js/MapWorks'
export default {
name: 'OfflineLayer',
mounted() {
this.init();
},
beforeDestroy(){
//实例被销毁前调用,页面关闭、路由跳转、v-if和改变key值
MapWorks.destroy();
},
methods:{
init(){
let container = document.getElementById("container");
MapWorks.initMap(container)
},
loadOfflineMap(){
MapWorks.loadOfflineMap()
}
}
}
</script>
<style lang="scss" scoped>
#container{
width: 100%;
height: 100%;
background: rgba(7, 12, 19, 1);
overflow: hidden;
background-size: 40px 40px, 40px 40px;
background-image: linear-gradient(hsla(0, 0%, 100%, 0.05) 1px, transparent 0), linear-gradient(90deg, hsla(0, 0%, 100%, 0.05) 1px, transparent 0);
.pane_container{
position: absolute;
left: 10px;
top: 50px;
padding: 10px 15px;
border-radius: 4px;
border: 1px solid rgba(128, 128, 128, 0.5);
color: #ffffff;
background: rgba(0, 0, 0, 0.4);
box-shadow: 0 3px 14px rgb(128 128 128 / 50%);
z-index: 2;
}
}
</style>