Vue2中配置Cesium全過程
更新時間:2023年05月19日 10:16:47 作者:愿為浪漫渡此劫
這篇文章主要介紹了Vue2中配置Cesium全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
基于Vue2配置Cesium
本文是關(guān)于基于Vue2,對Cesium,進(jìn)行在線使用和離線(內(nèi)網(wǎng))使用配置
一、安裝Cesium依賴
npm i Cesium
二、在線Cesimu配置(在vue.config.js文件中進(jìn)行如下配置)
const path = require("path"); const CopyWebpackPlugin = require("copy-webpack-plugin"); const webpack = require("webpack"); const cesiumSource = "./node_modules/cesium/Source"; function resolve(dir) { return path.join(__dirname, dir); } module.exports = { // 基本路徑 // publicPath: "./portal", // 打包 publicPath: "./", runtimeCompiler: true, // 輸出文件目錄 outputDir: "dist", configureWebpack: { output: { sourcePrefix: "", // 1 讓webpack 正確處理多行字符串配置 amd參數(shù) }, amd: { toUrlUndefined: true, // webpack在cesium中能友好的使用require }, resolve: { extensions: [".js", ".vue", ".json"], alias: { vue$: "vue/dist/vue.esm.js", "@": path.resolve("src"), components: path.resolve("src/components"), assets: path.resolve("src/assets"), views: path.resolve("src/views"), cesium: path.resolve(__dirname, cesiumSource), }, }, plugins: [ new CopyWebpackPlugin([ { from: path.join(cesiumSource, "Workers"), to: "Workers" }, ]), new CopyWebpackPlugin([ { from: path.join(cesiumSource, "Assets"), to: "Assets" }, ]), new CopyWebpackPlugin([ { from: path.join(cesiumSource, "Widgets"), to: "Widgets" }, ]), new CopyWebpackPlugin([ { from: path.join(cesiumSource, "ThirdParty/Workers"), to: "ThirdParty/Workers", }, ]), new webpack.DefinePlugin({ CESIUM_BASE_URL: JSON.stringify("./"), // 本地 //CESIUM_BASE_URL: JSON.stringify("./portal"), // 打包后 }), ], // 導(dǎo)致打包出現(xiàn)length undefined // // webpack在cesium中能友好的使用import.meta module: { rules: [ { test: /\.js$/, use: { loader: "@open-wc/webpack-import-meta-loader", }, }, ], } }, assetsDir: "assets", filenameHashing: false, lintOnSave: process.env.NODE_ENV !== "production", // lintOnSave: false, productionSourceMap: false, devServer: { host: "0.0.0.0", port: 8080, // 端口號 https: false, // https:{type:Boolean} open: true, //配置自動啟動瀏覽器 proxy: { "/pre": { target: "http://192.168.102.54:8733/", changeOrigin: true, pathRewrite: { "^/pre": "", }, }, }, }, css: { loaderOptions: { sass: { prependData: `@import "./src/assets/css/theme.scss";`, // scss 的目錄文件 }, }, }, };
三、離線Cesium配置
1、將Cesium資源文件夾
放在public/libs/Cesium,如圖所示
這個Cesium文件夾來源于,node-modules下的,如圖所示
2、單頁面 public/index.html引入
<script src="./libs/Cesium/Cesium.js" type="text/javascript"></script>
3、vue.config.js 如下配置
const path = require("path"); const CopyWebpackPlugin = require("copy-webpack-plugin"); const webpack = require("webpack"); const cesiumSource = "./node_modules/cesium/Source"; function resolve(dir) { return path.join(__dirname, dir); } module.exports = { // 基本路徑 // publicPath: "./portal", // 打包 publicPath: "./", runtimeCompiler: true, // 輸出文件目錄 outputDir: "dist", configureWebpack: { output: { sourcePrefix: "", // 1 讓webpack 正確處理多行字符串配置 amd參數(shù) }, amd: { toUrlUndefined: true, // webpack在cesium中能友好的使用require }, resolve: { extensions: [".js", ".vue", ".json"], alias: { vue$: "vue/dist/vue.esm.js", "@": path.resolve("src"), components: path.resolve("src/components"), assets: path.resolve("src/assets"), views: path.resolve("src/views"), cesium: path.resolve(__dirname, cesiumSource), }, }, plugins: [ new CopyWebpackPlugin([ { from: 'node_modules/cesium/Build/Cesium', to: 'libs/Cesium', filter: (resourcePath) => { if (/cesium[\\/]Build[\\/]Cesium[\\/]Cesium.js$/.test(resourcePath)) { return false; } else if (/cesium[\\/]Build[\\/]Cesium[\\/]Cesium.d.ts$/.test(resourcePath)) { return false; } return true; }, }, ]), new webpack.DefinePlugin({ // CESIUM_BASE_URL: JSON.stringify("./"), // 本地 CESIUM_BASE_URL: JSON.stringify('libs/Cesium'), //CESIUM_BASE_URL: JSON.stringify("./portal"), // 打包后 }), ], optimization : { splitChunks : { chunks : "all", cacheGroups : { cesium: { name: 'Cesium', priority: 30, test: /[\\/]node_modules[\\/]@smart[\\/]cesium[\\/]Build[\\/]Cesium[\\/]Cesium.js/ }, } } }, // 導(dǎo)致打包出現(xiàn)length undefined // // webpack在cesium中能友好的使用import.meta module: { rules: [ { test: /\.js$/, use: { loader: "@open-wc/webpack-import-meta-loader", }, }, ], } }, assetsDir: "assets", filenameHashing: false, lintOnSave: process.env.NODE_ENV !== "production", // lintOnSave: false, productionSourceMap: false, devServer: { host: "0.0.0.0", port: 8080, // 端口號 https: false, // https:{type:Boolean} open: true, //配置自動啟動瀏覽器 proxy: { "/sso": { target: "http://192.168.102.194:8098/", changeOrigin: true, //開啟代理:在本地會創(chuàng)建一個虛擬服務(wù)端,然后發(fā)送請求的數(shù)據(jù),并同時接收請求的數(shù)據(jù),這樣服務(wù)端和服務(wù)端進(jìn)行數(shù)據(jù)的交互就不會有跨域問題 pathRewrite: { "^/sso": "", //這里理解成用'/api'代替target里面的地址,比如我要調(diào)用'http://40.00.100.100:3002/user/add',直接寫'/api/user/add'即可 }, }, // 共享中心 "/pre": { target: "http://192.168.102.54:8733/", // zk // target: "http://192.168.102.43:8733/", // s changeOrigin: true, pathRewrite: { "^/pre": "", }, }, "/zk": { target: "http://192.168.102.54:8736/", changeOrigin: true, pathRewrite: { "^/zk": "", }, }, "/scene": { target: "http://192.168.102.43:8070/", changeOrigin: true, pathRewrite: { "^/scene": "", }, }, }, css: { loaderOptions: { sass: { prependData: `@import "./src/assets/css/theme.scss";`, // scss 的目錄文件 }, }, }, };
4、在Cesium初始化時
如下使用
init() { const Cesium = this.cesium; this.highlightColor = Cesium.Color.GREEN.withAlpha(0.6); this.normalColor = Cesium.Color.YELLOW.withAlpha(0.6); this.viewer = new Cesium.Viewer("cesiumContainer", { //先行禁止infoBox彈出 selectionIndicator: false, infoBox: false, geocoder: false, homeButton: false, sceneModePicker: false, fullscreenButton: false, navigationHelpButton: false, animation: false, timeline: false, fulllscreenButtond: false, vrButton: false, // 加載本地離線Cesium資源 imageryProvider: new Cesium.TileMapServiceImageryProvider({ url: Cesium.buildModuleUrl('Assets/Textures/NaturalEarthII'), }), }); this.viewer._cesiumWidget._creditContainer.style.display = "none"; // 隱藏版權(quán) },
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- vue中使用Cesium加載shp文件、wms服務(wù)、WMTS服務(wù)問題
- cesium開發(fā)之如何在vue項(xiàng)目中使用cesium,使用離線地圖資源
- vue中安裝使用cesium方式(親測可用)
- 使用開源Cesium+Vue實(shí)現(xiàn)傾斜攝影三維展示功能
- vue2.0項(xiàng)目集成Cesium的實(shí)現(xiàn)方法
- vue實(shí)現(xiàn)滾動條到頂部或者到指定位置
- Vue列表如何實(shí)現(xiàn)滾動到指定位置樣式改變效果
- vue keep-alive列表頁緩存 詳情頁返回上一頁不刷新,定位到之前位置
- vue?cesium加載點(diǎn)與定位到指定位置的實(shí)現(xiàn)方法
相關(guān)文章
vue+px2rem實(shí)現(xiàn)pc端大屏自適應(yīng)的實(shí)例代碼(rem適配)
不管是移動端的適配,還是大屏需求,都離不開不一個單位rem,rem是相對于根元素的字體大小的單位,下面這篇文章主要給大家介紹了關(guān)于vue+px2rem實(shí)現(xiàn)pc端大屏自適應(yīng)的相關(guān)資料,需要的朋友可以參考下2021-08-08vue-router2.0 組件之間傳參及獲取動態(tài)參數(shù)的方法
下面小編就為大家?guī)硪黄獀ue-router2.0 組件之間傳參及獲取動態(tài)參數(shù)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11Element 的 el-table 表格實(shí)現(xiàn)單元格合并功能
這篇文章主要介紹了Element 的 el-table 表格實(shí)現(xiàn)單元格合并功能,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-07-07vue打包通過image-webpack-loader插件對圖片壓縮優(yōu)化操作
這篇文章主要介紹了vue打包通過image-webpack-loader插件對圖片壓縮優(yōu)化操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11